round() doesn't seem to be rounding properly round() doesn't seem to be rounding properly python python

round() doesn't seem to be rounding properly


I can't help the way it's stored, but at least formatting works correctly:

'%.1f' % round(n, 1) # Gives you '5.6'


Formatting works correctly even without having to round:

"%.1f" % n


If you use the Decimal module you can approximate without the use of the 'round' function. Here is what I've been using for rounding especially when writing monetary applications:

from decimal import Decimal, ROUND_UPDecimal(str(16.2)).quantize(Decimal('.01'), rounding=ROUND_UP)

This will return a Decimal Number which is 16.20.