To check if a float is a whole number in Python, you can use is_integer()
method, isinstance()
method and create your integer check function. Follow the article to better understand.
Check if a float is a whole number in Python
The float number is a data type in Python. Float Python is a number represented as a floating point real number. You can simply understand that the float Python or float number data type is numbers containing decimals after the dot.
Example: 10.1
Integer data types in Python include positive integers, negative integers, and zero. Integers that do not contain commas.
Example: 1, 2, -11
To check if a float is a whole number in Python, I have the following ways:
Use is_integer() method
The is_integer()
is a built-in method of float data type, so you can use this method to check if any float number is an integer or not.
Syntax:
f.is_integer()
Parameters:
- f is the float number you need to check if it is an integer.
The is_integer()
method will return a boolean value.
Example:
- Initialize two variables as decimal point number types.
- The
is_integer()
function returns a Boolean value.
myFloat1 = 12.5 print(myFloat1.is_integer()) myFloat2 = 2.0 print(myFloat2.is_integer())
Output:
False
True
Use isinstance() method
The isinstance()
method checks if a specified number is an integer. This method is also similar to the is_integer()
method.
Syntax:
isinstance(object , classinfo)
Parameters:
- object: the number you want to check.
- classinfo: class, type, or tuple.
The isinstance()
method will return True for the specified type and False in all other cases.
Example:
- Initialize three variables 2 variables are decimals, the rest are integers.
- The
is_integer()
function returns a Boolean value.
myFloat1 = 2.0 myFloat2 = 2.1 myFloat3 = 2 # The isinstance() function returns a Boolean value print(isinstance(myFloat1, int)) print(isinstance(myFloat2, int)) print(isinstance(myFloat3, int))
Output:
False
False
True
Note:
The difference between isinstance()
function and is_integer()
function is that integers are written in decimal: the isinstance()
function defaults to a float, and the is_integer()
function gives it an integer. It would be best if you kept this in mind to use the methods accordingly.
That’s why myFloat1
= 2.0
returns False.
Create your integer check function
If you don’t use the built-in Python functions, you can write your function that can check if a float number is an integer using the math module.
Example:
import math def checkNumber(numParam): if math.ceil(numParam) != math.floor(numParam): print(numParam, " float") else: print(numParam, " integer") checkNumber(2.0) checkNumber(3.34)
Output:
2.0 integer
3.34 float
Summary
If you don’t know how to check if a float is a whole number in Python, let leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Check if a number is an int or float in Python
- Check if a string can be converted to a float in Python
- Convert NoneType to an Integer in Python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java