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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Aug 11 12:53:12 EDT 2007


On Sat, 11 Aug 2007 16:40:02 +0000, Adam W. wrote:

> 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?

If `str()` would not round you would get very long numbers because of the
inaccuracies of floating point values.  I know Python is lying when 0.1
prints as 0.1, but do you really want to see
0.10000000000000000555111512312578270211815834045410156250 instead?

Use string formatting to tell the number of digits you want to see:

In [16]: '%.56f' % 0.1
Out[16]: '0.10000000000000000555111512312578270211815834045410156250'

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list