Who told str() to round my int()'s!!!

Larry Bates larry.bates at websafe.com
Wed Aug 15 16:02:13 EDT 2007


Steve Holden wrote:
> Roel Schroeven wrote:
>> Adam W. schreef:
>>> After a fair amount of troubleshooting of why my lists were coming
>>> back a handful of digits short, and the last digit rounded off, I
>>> determined the str() function was to blame:
>>>
>>>>>> foonum
>>> 0.0071299720384678782
>>>>>> str(foonum)
>>> '0.00712997203847'
>>>
>>> Why in the world does str() have any business rounding my numbers, and
>>> how do I get around this?
>>
>> You could use repr() instead of str() (AFAIK that's what the
>> interactive interpreter used to print your foonum), but in any case
>> you should be aware of the limits inherent in floating point
>> arithmetic and in conversions between decimal and binary fractions.
>> See e.g. http://docs.python.org/tut/node16.html
>>
>>
> I should also point out that the subject line for this thread is
> inaccurate, as it wasn't rounding ints at all.
> 
> regards
>  Steve

What are they teaching in schools these days?  I see questions like this and the
equally perplexing "why don't floats represent numbers exactly?" or the mildy
amusing "how do I write bytes not characters to a file" questions at least once
a week on this forum.

-Larry



More information about the Python-list mailing list