10 examples of 'python round to 2 decimal' in Python

Every line of 'python round to 2 decimal' 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
this disclaimer
367def _round(self, value):
368 return value.quantize(self.exp)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

29@staticmethod
30def _round_decimal(number):
31 return number.quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
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
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'))
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
16def format_decimal(value):
17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
182def roundDownStr(n, dig):
183 shift = float(10 ** dig)
184 v = np.floor(n * shift) / shift
185 return ("%5." + str(dig) + "f") % v
238def test_as_decimal():
239 amount = EUR('1.23')
240 quantized_decimal = Decimal('1.23').quantize(Decimal('.01'))
241 assert amount.as_decimal() == quantized_decimal
59def truncate_decimal(val, places=2):
60 return trunc_decimal(val, places)
312def p_round(n, precision=5):
313 precision = int(precision)
314 return int(round(n / float(precision))) * precision

Related snippets