I need to check if a value is an integer in Python. Note that by integer I mean values like 2
, 1.0
and -4.0000
, whereas 0.4
and -2.3
are not integers.
How can I do this?
I need to check if a value is an integer in Python. Note that by integer I mean values like 2
, 1.0
and -4.0000
, whereas 0.4
and -2.3
are not integers.
How can I do this?
float
instances have an is_integer
method, which tells you whether f == int(f)
. The following snippet will therefore work for both integers and floats, as well as any strings representing either of those numerical types:
float(value).is_integer()