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

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Sat Aug 11 17:35:50 EDT 2007


Adam W. wrote:

> Why in the world does str() have any business rounding my numbers,

You are at the floating point numbers precision limit. Using str,
numbers are rounded to your machine's float precision in decimal
notation. Since floats are internally represented in binary
notation of constant precision, the decimal precision expressed in
number of places is not constant. Thus, safe rounding must be
applied to get consistent results.

> and how do I get around this?

If you don't want this (and thus introduce random deviations if you
assume greater precision than the number of places str gives you),
use repr. You've been warned.

>>> foonum = .0071299720384678782
>>> foonum
0.0071299720384678782
>>> str(foonum)
'0.00712997203847'
>>> repr(foonum)
'0.0071299720384678782'

Regards,


Björn

-- 
BOFH excuse #5:

static from plastic slide rules




More information about the Python-list mailing list