[Python-Dev] rounding problem

Tony Meyer t-meyer at ihug.co.nz
Mon May 31 21:41:27 EDT 2004


[I think this would be better in python-list, not python-dev]

> the number actually comes from multiplication between
> 237.50 and 4.762 which will get 1130.975. So it suppose
> to be 1130.98 isnt it if it'll get rounded to 2 decimal
> places. 

Gary is correct:

>>> 237.50 * 4.762
1130.9749999999999
>>> round(_,2)
1130.97
>>> print 237.50 * 4.762
1130.975

The answer correctly rounds to 1130.97 because the next digit is 4, not 5.
When you print it, it rounds to 3 decimal places, so you get the .9749
rounding to .975.

This is just floating point arithmetic.  You might want to try the Decimal
module, or something like that, to avoid this.

=Tony Meyer




More information about the Python-Dev mailing list