Formatting numbers

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed May 2 04:20:39 EDT 2001


Daniel Klein <danielk at aracnet.com> wrote in
<96quet06pde16eod4i1ntvl38rbnt554r0 at 4ax.com>: 

> I wonder why this works cos 'x' sometimes shows up as a float with a
> large decimal expansion, ie
> 
>     >>> x = 123456
>     >>> x/100.0
>      1234.5599999999999
>     >>> str(x/100.0)
>      '1234.56'
>     >>> 
> 
The difference is because the value printed when you type an expression 
interactively is the repr() of the value, not the str() (in Python 1.5x it 
was the str that was printed).

    	>>> x = 1234.56
    	>>> x
    	1234.5599999999999
    	>>> print str(x)
    	1234.56
    	>>> print repr(x)
    	1234.5599999999999
    	>>>

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list