Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ROUND_HALF_UP not working as expected

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

like image 390
Brownbay Avatar asked Mar 24 '14 02:03

Brownbay


1 Answers

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
like image 50
paxdiablo Avatar answered Sep 20 '22 20:09

paxdiablo