I want a
to be rounded to 13.95.
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
The ro
It's simple like 1,2,3:
use decimal module for fast correctly-rounded decimal floating point arithmetic:
d=Decimal(10000000.0000009)
to achieve rounding:
d.quantize(Decimal('0.01'))
will results with Decimal('10000000.00')
def round_decimal(number, exponent='0.01'):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(exponent))
OR
def round_decimal(number, decimal_places=2):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(10) ** -decimal_places)
PS: critique of others: formatting is not rounding.