How do I check that a number is float or integer?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Checking whether a number is a float or an integer sounds simple, but the correct answer depends on the language and on whether you mean the variable's type or the numeric value it currently holds. Those are not always the same question.
Type Versus Value
A variable can have:
- an integer type
- a floating-point type that happens to hold a whole-number value
For example, 7 and 7.0 may compare numerically the same, but their types are different in many languages. Good code decides which question matters first.
Python
In Python, checking the actual type is straightforward:
But if you want to know whether a float holds an integer-valued number:
That distinction matters a lot. 7.0 is still a float, even though its value is mathematically an integer.
JavaScript
JavaScript has a different wrinkle: ordinary numbers are all number values, whether they look like integers or decimals. So typeof alone does not solve the problem. Use Number.isInteger() when the question is about the value:
If you need a distinct integer type in JavaScript, that becomes a different discussion involving BigInt.
C#
In C#, type checking works directly:
If the variable is already strongly typed, the compiler often makes the question almost trivial because the type is part of the declaration.
That said, parsing is still separate from type checking. A value read from a text box or a file is often a string first, so the real workflow is usually: parse, validate, then check whether the parsed result is allowed to contain a fractional part.
Skipping that order causes many avoidable bugs.
It happens surprisingly often in practice.
Why This Matters
Programs care about this distinction for validation, serialization, UI formatting, and calculations. A JSON payload may allow only integers. A reporting tool may display 7 differently from 7.0. A parser may need to reject decimal input even when it can be rounded cleanly.
That is why "check if number is float or integer" is rarely just academic. The answer usually affects program behavior.
It also affects how you handle user input. A text field containing "7" might be parsed differently from "7.0" depending on the rules of the application, even if both ultimately represent the same mathematical quantity.
That is why validation code should state its intent clearly.
Otherwise the check becomes ambiguous very quickly.
Common Pitfalls
- Confusing numeric value with variable type.
- Using
typeofin JavaScript and expecting separate integer and float results. - Treating
7.0as an integer type in Python just becauseis_integer()returns true. - Forgetting that user input often starts as text and must be parsed first.
- Applying the same rule across languages with different numeric models.
Summary
- Decide whether you care about the value or the type.
- In Python,
isinstancechecks the type andis_integer()checks float-valued wholes. - In JavaScript, use
Number.isInteger()because normal numbers share onenumbertype. - In strongly typed languages such as C#, type checks are usually direct.
- The right answer depends on the language and the exact validation goal.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.