Python
Programming
Floating Point
Whole Numbers
Code Tips

How to check if a float value is a whole number

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A float value like 7.0 represents a whole number, but your program still treats it as a float. Whether you are validating user input, checking computation results, or deciding how to format a number for display, you often need to determine if a float is actually a whole number (has no fractional part). Different programming languages provide different ways to do this. This article covers practical methods in Python, JavaScript, Java, and C++, along with the floating-point precision issues you need to watch out for.

Python

Python offers a built-in method on float objects specifically for this check.

Method 1: float.is_integer()

The most Pythonic approach. The is_integer() method returns True if the float has no fractional component.

python
1x = 7.0
2y = 7.5
3
4print(x.is_integer())  # True
5print(y.is_integer())  # False

This method handles edge cases well, including negative numbers and zero.

python
print((-3.0).is_integer())  # True
print((0.0).is_integer())   # True

Method 2: Modulo Operation

You can check if the remainder when divided by 1 is zero.

python
1def is_whole(value):
2    return value % 1 == 0
3
4print(is_whole(4.0))   # True
5print(is_whole(4.3))   # False

Method 3: Comparing int Conversion

Cast the float to an integer and compare it back to the original.

python
1def is_whole(value):
2    return float(int(value)) == value
3
4print(is_whole(9.0))   # True
5print(is_whole(9.1))   # False

This approach can fail for very large floats that exceed integer precision, so is_integer() is generally preferred.

JavaScript

JavaScript has a dedicated method on the Number object.

Number.isInteger()

javascript
console.log(Number.isInteger(5.0));  // true
console.log(Number.isInteger(5.5));  // false
console.log(Number.isInteger(5));    // true

Note that JavaScript does not have separate integer and float types. All numbers are IEEE 754 doubles. Number.isInteger() checks whether the value has no fractional part and is within safe integer range.

Modulo Check

javascript
1function isWhole(value) {
2  return value % 1 === 0;
3}
4
5console.log(isWhole(8.0));  // true
6console.log(isWhole(8.2));  // false

Use strict equality (===) here to avoid type coercion surprises.

Java

Java provides a straightforward approach using the modulo operator or Math.floor.

Modulo with Double

java
1public class WholeNumberCheck {
2    public static boolean isWhole(double value) {
3        return value % 1 == 0;
4    }
5
6    public static void main(String[] args) {
7        System.out.println(isWhole(6.0));   // true
8        System.out.println(isWhole(6.7));   // false
9    }
10}

Using Math.floor

java
public static boolean isWhole(double value) {
    return Math.floor(value) == value;
}

Both approaches work for normal values. For special cases like Double.NaN or Double.POSITIVE_INFINITY, add explicit checks.

java
public static boolean isWhole(double value) {
    return !Double.isNaN(value) && !Double.isInfinite(value) && value % 1 == 0;
}

C++

C++ offers functions from the <cmath> header.

Using std::floor

cpp
1#include <cmath>
2#include <iostream>
3
4bool isWhole(double value) {
5    return std::floor(value) == value;
6}
7
8int main() {
9    std::cout << std::boolalpha;
10    std::cout << isWhole(3.0) << std::endl;  // true
11    std::cout << isWhole(3.4) << std::endl;  // false
12    return 0;
13}

Using std::fmod

cpp
1#include <cmath>
2#include <iostream>
3
4bool isWhole(double value) {
5    return std::fmod(value, 1.0) == 0.0;
6}

Using std::trunc (C++11)

cpp
bool isWhole(double value) {
    return std::trunc(value) == value;
}

All three approaches are equivalent for finite values. Add guards for NaN and infinity using std::isfinite().

Handling Floating-Point Precision

Floating-point arithmetic can produce results that are extremely close to a whole number but not exactly equal. For example, 0.1 + 0.2 produces 0.30000000000000004 in most languages, not 0.3. After a chain of arithmetic operations, a value that should be 5.0 might end up as 4.999999999999999.

To handle this, use a tolerance-based comparison.

python
1import math
2
3def is_whole_approx(value, tol=1e-9):
4    return math.isclose(value - round(value), 0, abs_tol=tol)
5
6print(is_whole_approx(4.999999999999999))  # True
7print(is_whole_approx(4.5))                # False
javascript
function isWholeApprox(value, tol = 1e-9) {
  return Math.abs(value - Math.round(value)) < tol;
}

Choose the tolerance based on your domain. For financial calculations, you might use 1e-2. For scientific computing, 1e-12 or smaller may be appropriate.

Common Pitfalls

  1. Ignoring precision errors. Directly comparing value % 1 == 0 fails when floating-point arithmetic produces values like 2.9999999999999996 instead of 3.0. Use a tolerance-based check when the value is the result of prior computations.
  2. Forgetting NaN and Infinity. NaN % 1 is NaN, and Infinity % 1 is NaN. Neither is a whole number, but a naive comparison may not catch them. Check for these special values explicitly.
  3. Integer overflow on cast. Converting a very large float (like 1e20) to an integer can overflow in languages with fixed-size integers (C++, Java). The int() cast in Python handles arbitrary precision, but other languages do not.
  4. Using == with floats carelessly. Exact equality checks on floating-point numbers are unreliable after arithmetic operations. Reserve exact checks for values that were assigned directly (like constants), and use tolerance for computed results.

Summary

To check if a float is a whole number, use the language's built-in method when available: float.is_integer() in Python, Number.isInteger() in JavaScript, or modulo/floor comparisons in Java and C++. When the value comes from arithmetic operations, use a tolerance-based comparison to account for floating-point imprecision. Always guard against special values like NaN and Infinity in statically typed languages.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.