10 examples of 'python round to 4 decimal places' in Python

Every line of 'python round to 4 decimal places' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
631def rpy_round(number, ndigits):
632 # Algorithm copied directly from CPython
633
634 if number == 0 or rfloat.isinf(number) or rfloat.isnan(number):
635 return number
636
637 # Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
638 # always rounds to itself. For ndigits < NDIGITS_MIN, x always
639 # rounds to +-0.0.
640 if ndigits > NDIGITS_MAX:
641 return number
642 elif ndigits < NDIGITS_MIN:
643 # return 0.0, but with sign of x
644 return 0.0 * number
645
646 # finite x, and ndigits is not unreasonably large
647 z = rfloat.round_double(number, ndigits)
648 if rfloat.isinf(z):
649 raise OverflowError
650 return z
7def testRoundingDecimals(self):
8 """Test Partial Unit Rounding Decimal Conversion behavior"""
9 val = round_decimal(val=3.40, places=5, roundfactor=.5, normalize=True)
10 self.assertEqual(val, Decimal("3.5"))
11
12 val = round_decimal(val=3.40, places=5, roundfactor=-.5, normalize=True)
13 self.assertEqual(val, Decimal("3"))
14
15 val = round_decimal(val=0, places=5, roundfactor=-.5, normalize=False)
16 self.assertEqual(val, Decimal("0.00000"))
17
18 val = round_decimal(0, 5, -.5, False)
19 self.assertEqual(val, Decimal("0.00000"))
20
21 val = round_decimal(0)
22 self.assertEqual(val, Decimal("0"))
23
24 val = round_decimal(3.23,4,-.25)
25 self.assertEqual(val, Decimal("3"))
26
27 val = round_decimal(-3.23,4,-.25)
28 self.assertEqual(val, Decimal("-3"))
29 val = round_decimal(-3.23,4,.25)
30 self.assertEqual(val, Decimal("-3.25"))
31
32 val = round_decimal(3.23,4,.25)
33 self.assertEqual(val, Decimal("3.25"))
34
35 val = round_decimal(3.23,4,.25,False)
36 self.assertEqual(val, Decimal("3.2500"))
37
38 val = round_decimal(3.23,1,.25,False)
39 self.assertEqual(val, Decimal("3.2"))
40
41 val = round_decimal(2E+1, places=2)
42 self.assertEqual(val, Decimal('20.00'))
419def roundValue(value, precision=None, decimals=None, scale=None):
420 try:
421 vDecimal = decimal.Decimal(value)
422 if scale:
423 iScale = int(scale)
424 vDecimal = vDecimal.scaleb(iScale)
425 if precision is not None:
426 vFloat = float(value)
427 if scale:
428 vFloat = pow(vFloat, iScale)
429 except (decimal.InvalidOperation, ValueError): # would have been a schema error reported earlier
430 return NaN
431 if precision is not None:
432 if not isinstance(precision, (int,float)):
433 if precision == "INF":
434 precision = floatINF
435 else:
436 try:
437 precision = int(precision)
438 except ValueError: # would be a schema error
439 precision = floatNaN
440 if isinf(precision):
441 vRounded = vDecimal
442 elif precision == 0 or isnan(precision):
443 vRounded = NaN
444 elif vFloat == 0:
445 vRounded = ZERO
446 else:
447 vAbs = fabs(vFloat)
448 log = log10(vAbs)
449 d = precision - int(log) - (1 if vAbs >= 1 else 0)
450 vRounded = decimalRound(vDecimal,d,decimal.ROUND_HALF_UP)
451 elif decimals is not None:
452 if not isinstance(decimals, (int,float)):
453 if decimals == "INF":
454 decimals = floatINF
455 else:
456 try:
457 decimals = int(decimals)
458 except ValueError: # would be a schema error
459 decimals = floatNaN
460 if isinf(decimals):
461 vRounded = vDecimal
462 elif isnan(decimals):
463 vRounded = NaN
464 else:
465 vRounded = decimalRound(vDecimal,decimals,decimal.ROUND_HALF_EVEN)
466 else:
467 vRounded = vDecimal
468 return vRounded
367def _round(self, value):
368 return value.quantize(self.exp)
312def p_round(n, precision=5):
313 precision = int(precision)
314 return int(round(n / float(precision))) * precision
182def roundDownStr(n, dig):
183 shift = float(10 ** dig)
184 v = np.floor(n * shift) / shift
185 return ("%5." + str(dig) + "f") % v
29@staticmethod
30def _round_decimal(number):
31 return number.quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
700@staticmethod
701@pytest.mark.parametrize(params, tuple(zip(*inputs, data['rounddown'])))
702def test_rounddown(number, digits, result):
703 assert result == rounddown(number, digits)
17def round_up(x, base=1):
18 return int(base * round(float(x)/base))
163def HumanReadableWithDecimalPlaces(number, decimal_places=1):
164 """Creates a human readable format for bytes with fixed decimal places.
165
166 Args:
167 number: The number of bytes.
168 decimal_places: The number of decimal places.
169 Returns:
170 String representing a readable format for number with decimal_places
171 decimal places.
172 """
173 number_format = MakeHumanReadable(number).split()
174 num = str(int(round(10**decimal_places * float(number_format[0]))))
175 if num == '0':
176 number_format[0] = ('0' +
177 (('.' +
178 ('0' * decimal_places)) if decimal_places else ''))
179 else:
180 num_length = len(num)
181 if decimal_places:
182 num = (num[:num_length - decimal_places] + '.' +
183 num[num_length - decimal_places:])
184 number_format[0] = num
185 return ' '.join(number_format)

Related snippets