Categories
Python Answers

How to check if a float value is a whole number with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *