Sometimes, we want to check if a float value is a whole number with Python.
In this article, we’ll look at how to check if a float value is a whole number with Python.
How to check if a float value is a whole number with Python?
To check if a float value is a whole number with Python, we can use the float’s is_integer
method.
For instance, we write:
print((1.0).is_integer())
print((1.5).is_integer())
We call is_integer
on 2 floats and print out the values.
Since 1.0 is an integer, the first print
will print True
.
And since 1.5 isn’t an integer, the 2nd print
will print False
.
Conclusion
To check if a float value is a whole number with Python, we can use the float’s is_integer
method.