[Tutor] Limitation of int() in converting strings
Alan Gauld
alan.gauld at btinternet.com
Mon Dec 17 09:55:05 CET 2012
On 17/12/12 04:19, boB Stepp wrote:
> It is apparent that int() does not like strings with floating-point
> formats. None of my books (as far as my flipping can tell) or the
> below built-in help clarify this:
> ...
> Of course if I type int(float('10.0')) I get the desired 10 .
as indeed will
int(10.0)
So you are right, there is an inconsistency between how int() converts
floating point numbers and how it converts strings. Even stranger since
the underlying atoi() C function appears to handle float strings quite
happily...
> So, I am guessing that to convert strings to integers with int() that
> the string must already be of integer format? What is the rationale
> for setting up int() in this manner?
No idea, you'd need to ask Guido, it's his language :-)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list