int('0.0') throws an exception

Andrew Dalke adalke at mindspring.com
Wed Jan 29 19:44:57 EST 2003


Hi Ragu,

> Why is it that
> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

Because it doesn't?

int(string) already supports
   - leading spaces
   - signs
   - optional bases, like int("1Z", 36) == 71
   - using a base of 0 will look for octal and hex numbers

int(float) works for any float, to turn it into an int.


Should int(string) work when the string is a float representation?
That is, would you also allow the following?

   int("9.85638E+3") == 9856

I would hope not, since anything which goes through a float but
isn't a float gives me the heebie-jeebies.


You could write such a thing yourself, as in

def my_int(s):
   try:
     return int(s)
   except ValueError:
     return int(float(s))

But why don't you just do int(float(s)) ?

					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list