• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Check if Float is a Whole Number

Python Check if Float is a Whole Number

February 17, 2022 Leave a Comment

The easiest way to check if a number is a whole number in Python is using the is_integer() function.

print((2.0).is_integer())
print((2.01).is_integer())

#Output:
True
False

You can also check if the number minus the integer conversion of the number is equal to 0.

print(2.0 - int(2.0) == 0)
print(2.01 - int(2.01) == 0)

#Output:
True
False

In Python, when working with numbers, it can be useful to be able to find out if a number is a whole number.

We can easily check if a number is a whole number with the help of the float is_integer() function.

The is_integer() function returns a boolean indicating if the float is an integer or not.

Below are some examples of how to use the Python is_integer() function to check if a number is a whole number.

print((2.0).is_integer())
print((2.01).is_integer())

#Output:
True
False

Checking if a Number is a Whole Number Using Integer Conversion in Python

You can also check if a number is a whole number using integer conversion in Python. If we convert a floating point number to an integer, then the difference between the number and the newly created integer should be 0 if the number is a whole number.

Below are some examples of how you can use integer conversion to check if number is whole.

print(2.0 - int(2.0) == 0)
print(2.01 - int(2.01) == 0)

#Output:
True
False

Other Articles You'll Also Like:

  • 1.  Python math.factorial() function – Calculate Factorial of Number
  • 2.  Using Python to Remove First Character from String
  • 3.  How to Check if Set is Empty in Python
  • 4.  Creating a Random Color Turtle in Python
  • 5.  Python Destroy Object – How to Delete Objects with del Keyword
  • 6.  Remove Trailing Zeros from String with rstrip() in Python
  • 7.  Get Quarter from Date in pandas DataFrame
  • 8.  Python Subtract Days from Date Using datetime timedelta() Function
  • 9.  Backwards for Loop in Python
  • 10.  PROC REG Equivalent in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy