Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Check if a Given Number is Fibonacci number - Python
Next article icon

Check if a Number is a Whole Number in Python

Last Updated : 25 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Floating-point numbers in Python can sometimes pose challenges when you need to determine whether they represent whole numbers. Due to the inherent precision limitations of floating-point representation, comparing them directly for equality with integers may lead to unexpected results. In this article, we will explore some different methods to check if a float value is a whole number in Python.

Check If A Float Value Is A Whole Number

Below, we will explain some methods of How To Check If A Float Value Is A Whole Number in Python.

  • Using the is_integer()
  • Comparing with int()
  • Using the % Operator
  • Using math.isclose()

Check If A Float Value Is A Whole Number Using the is_integer()

In this example, below Python code below defines a function `is_whole_number_method1` that takes a number as input and returns `True` if the number is a whole number (integer), using the `is_integer()` method.

Python3
def is_whole_number_method1(number):     return number.is_integer()  # Example float_value = 7.0 result = is_whole_number_method1(float_value) print(f"{float_value} is a whole number: {result}") 

Output
7.0 is a whole number: True 

Check If A Float Value Is A Whole Number Comparing with int()

In this example, below Python code defines a function `is_whole_number_method2` that checks if a number is a whole number by comparing it to its integer conversion. The example uses this method to verify if the float value 9.5 is a whole number and prints the result.

Python3
def is_whole_number_method2(number):     return number == int(number)  # Example float_value = 9.5 result = is_whole_number_method2(float_value) print(f"{float_value} is a whole number: {result}") 

Output
9.5 is a whole number: False 

Check If A Float Value Is A Whole Number Using the % Operator

In this example, below Python code defines a function `is_whole_number_method3` that determines if a number is a whole number by checking if the remainder of its division by 1 is equal to 0. The example uses this method to evaluate if the float value 3.14 is a whole number and prints the result.

Python3
def is_whole_number_method3(number):     return number % 1 == 0  # Example float_value = 3.14 result = is_whole_number_method3(float_value) print(f"{float_value} is a whole number: {result}") 

Output
3.14 is a whole number: False 

Check If A Float Value Is A Whole Number Using math.isclose()

In this example, below Python code introduces a function `is_whole_number_method4` that utilizes the `math.isclose` function to check if a number is approximately equal to its rounded version, thereby determining if it is a whole number. The example applies this method to assess if the float value 10.0001 is a whole number and prints the result.

Python3
import math  def is_whole_number_method4(number):     return math.isclose(number, round(number))  # Example float_value = 10.000 result = is_whole_number_method4(float_value) print(f"{float_value} is a whole number: {result}") 

Output
10.0 is a whole number: True 

Conclusion

In conclusion, the provided Python code offers four different methods to check if a float value is a whole number. Each method leverages distinct approaches, such as utilizing built-in methods like is_integer(), direct comparison with the integer conversion, checking the remainder of division by 1, and employing the math.isclose function for approximate equality with the rounded version.


Next Article
How to Check if a Given Number is Fibonacci number - Python

B

bytebarde55
Improve
Article Tags :
  • Python
  • Python Programs
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

  • Python Program to Check If a Number is a Harshad Number
    Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
    2 min read
  • How to Check if a Given Number is Fibonacci number - Python
    Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Notice that every number is equal to the sum of its previous 2 numbers. In this article, we will learn how
    2 min read
  • Python program to check if the given number is Happy Number
    A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with a Happy Number and keep replacing it with digits square sum, we reach 1. In this article, we will check if the given number is a Happy
    4 min read
  • Python Check If String is Number
    In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number. Example: Using isdigit() Method [GFGTABS] Python # Python code to check if strin
    6 min read
  • Check Prime Number in Python
    Given a positive integer N, the task is to write a Python program to check if the number is Prime or not in Python. For example, given a number 29, it has no divisors other than 1 and 29 itself. Hence, it is a prime number. Note: Negative numbers (e.g. -13) are not considered prime number. Using sym
    7 min read
  • Python Program to Check Number is a Power of Two
    we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer. Examples: [GFGTABS] Python def is_power_of_two(n): if n <= 0: return False return
    4 min read
  • Python Program to Check if a Number is Odd or Even
    Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1. In this article, we will learn how to check i
    2 min read
  • Python program to check if number is palindrome (one-liner)
    In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
    3 min read
  • Python | Check If A Given Object Is A List Or Not
    Given an object, the task is to check whether the object is a list or not. Python provides few simple methods to do this and in this article, we'll walk through the different ways to check if an object is a list: Using isinstance()isinstance() function checks if an object is an instance of a specifi
    2 min read
  • Python - Check if String contains any Number
    We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences