How to check if a float value is a whole number
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.
This method handles edge cases well, including negative numbers and zero.
Method 2: Modulo Operation
You can check if the remainder when divided by 1 is zero.
Method 3: Comparing int Conversion
Cast the float to an integer and compare it back to the original.
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()
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
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
Using Math.floor
Both approaches work for normal values. For special cases like Double.NaN or Double.POSITIVE_INFINITY, add explicit checks.
C++
C++ offers functions from the <cmath> header.
Using std::floor
Using std::fmod
Using std::trunc (C++11)
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.
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
- Ignoring precision errors. Directly comparing
value % 1 == 0fails when floating-point arithmetic produces values like2.9999999999999996instead of3.0. Use a tolerance-based check when the value is the result of prior computations. - Forgetting NaN and Infinity.
NaN % 1isNaN, andInfinity % 1isNaN. Neither is a whole number, but a naive comparison may not catch them. Check for these special values explicitly. - Integer overflow on cast. Converting a very large float (like
1e20) to an integer can overflow in languages with fixed-size integers (C++, Java). Theint()cast in Python handles arbitrary precision, but other languages do not. - 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.

