[Tutor] Rounding off

Kent Johnson kent37 at tds.net
Sun Mar 18 14:51:24 CET 2007


Dick Moores wrote:

>>  >>> print round(2.1345, 3)
>> 2.135
> 
> But now I have a question.
> 
>  >>> print "%.3f" % 22.1345
> 22.134
> 
> Why the 22.134's?

Because neither 2.1345 nor 22.1345 can be represented exactly in 
floating point, and the errors are in the opposite direction:

In [4]: repr(22.1345)
Out[4]: '22.134499999999999'
In [5]: repr(2.1345)
Out[5]: '2.1345000000000001'

Kent


More information about the Tutor mailing list