Floating point -> string conversions
Steve Holden
steve at holdenweb.com
Thu Nov 11 17:53:19 EST 2004
John Fouhy wrote:
> I have another related question...
>
>
>>>>pow(2, 31)
>
> 2147483648L
>
>>>>'%d' % 2147483647.0 # python will convert to int
>
> '2147483647'
>
>>>>'%d' % 2147483648.0 # too big for an int, so error.
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: int argument required
>
>>>>'%d' % long(2147483648.0) # but yet, no trouble accepting a long.
>
> '2147483648'
>
>>>>'%d' % int(2147483648.0) # and int() converts to long anyway
>
> '2147483648'
>
> Is this a bug?
>
> (python 2.3.4)
>
I don't think so. The fact of the matter is that a %d format token
explicitly expects an integral value.
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?
regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
More information about the Python-list
mailing list