[BangPypers] why is round() not working?

Asokan Pichai pasokan at gmail.com
Fri Feb 19 08:28:03 CET 2010


On Fri, Feb 19, 2010 at 12:37 PM, Kenneth Gonsalves <lawgon at au-kbc.org> wrote:
> hi
>
>>>> round(2.1667000000000001,3)
> 2.1669999999999998
>
> what gives?
>
[snip]

You are running into the problems of floating point number
representation. This has nothing to do with round()

>>> x = 2.17
>>> x
2.1699999999999999
>>>

The number displayed is the best representation given the binary
representation's limits of storage etc., Some languages (Ruby comes to
mind) display 'better' looking results and IIRC Python 3 will also do
the same.

$ irb
>> x = 2.17
=> 2.17
>> x
=> 2.17
>>

If you are looking at displaying, consider doing sth. like

>>> print "%5.3f" %(round(2.16670000001, 3))
2.167

HTH
-- 
Asokan Pichai
*-------------------*
We will find a way. Or, make one. (Hannibal)


More information about the BangPypers mailing list