round(22.47,2) gives 22.469999999999999?

Thomas Andrews thomaso at best.com
Wed Aug 6 07:38:09 EDT 2003


Eric van Riet Paap wrote:

> Hi,
> 
> On python2.1.3, python2.2.1 and python2.2.3 round(22.47,2) gives 
> 22.469999999999999 . Does anyone know if this is a bug or some weird, yet 
> unexpected, behaviour?
> 
> P.S. I haven't tested python2.3, but python1.5.2 doesn't have this problem.
> 

In IDLE, just typing:

 >>> 22.47
22.469999999999999

That seems to indicate that 22.47 is best approximated by the floating 
number 22.499999999999.

Rounding should probably not be used for gettting "formatable" values. 
Instead, you can do something like:

 >>> "%.2f" % 22.47
'22.47'

if you want a string version of the number rounded to 2 decimal places.

=thomas







More information about the Python-list mailing list