Floating point -> string conversions

Andrew A. Cox Andrew at ferndale.net.nz
Sun Nov 21 21:20:08 CET 2004

I disagree with '%d' you have explicitly requested that the number be
turned into an integer for use as a string, so you expect that as long
as you feed it a number (be it int, long or float) that it changes to a
string representation of an integer.  Now it may be nice to have a flag
that says how you want to round it.

-----Original Message-----
From: John Fouhy [mailto:jfouhy at paradise.net.nz] 
Sent: Friday, 19 November 2004 2:47 p.m.
To: python-list at python.org
Subject: Re: Floating point -> string conversions

Steve Holden <steve at holdenweb.com> wrote in message
news:<t9Skd.57$nj.56 at lakeread01>...
> The fact that %s coerces things makes us expect more of other format 
> tokens, but what would you expect %d to do with 2147483648.5?

I'd expect it to give me "2147483648", since that would be consistent
with its other behaviour :-)

(eg: eval('"%d" % 1.5') == '1')

I guess what annoys me is that python encourages you to not worry
about the difference between ints and longs (which is great) (unless
you're doing high performance computing), but then something like this
can cause code to stop working just because you've passed a border
which is almost invisible.

(of course, this means that one solution which I would be happy with
is for python to reject any non-integer/long arguments to "%d" :-) )


More information about the Python-list mailing list