<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2658.2">
<TITLE>Floating point -> string conversions</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>There's a thread in the standard list (with this very Subject) that talks about implicit coercing with "%d" in strings replacement.</FONT></P>
<P><FONT SIZE=2>jfouhy posted this example:</FONT>
</P>
<P><FONT SIZE=2>>>> pow(2, 31)</FONT>
<BR><FONT SIZE=2>2147483648L</FONT>
<BR><FONT SIZE=2>>>> '%d' % 2147483647.0 # python will convert to int</FONT>
<BR><FONT SIZE=2>'2147483647'</FONT>
<BR><FONT SIZE=2>>>> '%d' % 2147483648.0 # too big for an int, so error.</FONT>
<BR><FONT SIZE=2>Traceback (most recent call last):</FONT>
<BR><FONT SIZE=2> File "<stdin>", line 1, in ?</FONT>
<BR><FONT SIZE=2>TypeError: int argument required</FONT>
<BR><FONT SIZE=2>>>> '%d' % long(2147483648.0) # but yet, no trouble accepting a long.</FONT>
<BR><FONT SIZE=2>'2147483648'</FONT>
<BR><FONT SIZE=2>>>> '%d' % int(2147483648.0) # and int() converts to long anyway</FONT>
<BR><FONT SIZE=2>'2147483648'</FONT>
</P>
<BR>
<P><FONT SIZE=2>I think that here's a bug, but don't know where:</FONT>
</P>
<P><FONT SIZE=2>Should %d accept only integer values? So this is a bug:</FONT>
</P>
<P><FONT SIZE=2>>>> '%d' % 2147483647.0</FONT>
<BR><FONT SIZE=2>'2147483647'</FONT>
</P>
<P><FONT SIZE=2>Should %d accept also floats, and make the conversion? So there're two bugs:</FONT>
</P>
<P><FONT SIZE=2>- In the doc (it's not explicited that it should accept a other data types)</FONT>
</P>
<P><FONT SIZE=2>- In this behaviour difference:</FONT>
</P>
<P><FONT SIZE=2>>>> '%d' % 2147483647.0</FONT>
<BR><FONT SIZE=2>'2147483647'</FONT>
<BR><FONT SIZE=2>>>> '%d' % 2147483648.0</FONT>
<BR><FONT SIZE=2>Traceback (most recent call last):</FONT>
<BR><FONT SIZE=2>...</FONT>
<BR><FONT SIZE=2>TypeError: int argument required</FONT>
</P>
<BR>
<P><FONT SIZE=2>I really don't know about opening a bug, because I don't know which to open, :p</FONT>
</P>
<P><FONT SIZE=2>. Facundo</FONT>
</P>
</BODY>
</HTML>