Best way to check if string is an integer?

Stephen Cattaneo Stephen.Cattaneo at u4eatech.com
Tue Apr 8 16:45:50 EDT 2008


1E+1 is short hand for a floating point number, not an interger.
>>> float("1E+1")
10.0

You could convert the float to an integer if you wanted (i.e. ceiling,
floor, rounding, truncating, etc.).

Cheers,

Steve

-----Original Message-----
From: Martin Marcher [mailto:martin at marcher.name] 
Sent: Tuesday, April 08, 2008 1:32 PM
To: python-list at python.org
Subject: Re: Best way to check if string is an integer?

hmmm

int() does miss some stuff:

>>> 1E+1
10.0
>>> int("1E+1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1E+1'

I wonder how you parse this?

I honestly thought until right now int() would understand that and
wanted to show that case as ease of use, I was wrong, so how do you
actually cast this type of input to an integer?

thanks
martin


-- 
http://tumblr.marcher.name
https://twitter.com/MartinMarcher
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.




More information about the Python-list mailing list