I can't seem to understand the decimal docs to get 2.775 to round to 2.78 using the decimal class.
import decimal
decimal.getcontext().prec = 7
print(decimal.Decimal(2.775).quantize(
decimal.Decimal('0.00'),
rounding=decimal.ROUND_HALF_UP
))
>> 2.77 # I'm expecting 2.78
That SHOULD round to 2.78, but I keep getting 2.77.
EDIT: Testing on python 3.4
If you add a single line to your code thus:
print (decimal.Decimal(2.775))
then you'll see why it's rounding down:
2.774999999999999911182158029987476766109466552734375
The value 2.775
, as a literal is stored as a double, which has limited precision.
If you instead specify it as a string, the value will be preserved more accurately:
>>> import decimal
>>> print (decimal.Decimal(2.775))
2.774999999999999911182158029987476766109466552734375
>>> print (decimal.Decimal('2.775'))
2.775
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With