How do I check that a number is float or integer?
Swift determination of whether a number is float or integer can be achieved as simple as this: check (num % 1) !== 0
for floats, or use Number.isInteger(num)
for integers. Here's how to put them into action:
Fundamentals of integers and floats
A precise understanding of what differentiates a float from an integer will build a solid foundation. An integer is a number without any decimal points—whole numbers, if you prefer. On the flip side, a float (or floating-point number) does have a decimal part—even if that decimal part is zero (e.g., 5.0
).
Methods for distinguishing number types
JavaScript offers several methods to facilitate the distinction between integers and floats. Let's navigate through these, keeping an eye out for those pesky edge cases along the way.
Ensuring the value is numeric
Initially, it is crucial to verify if the given value is numeric. Here's one way of doing it:
Polyfill for Number.isInteger()
ES5 doesn't support Number.isInteger()
. Talk about feeling left out! Hence, if you have to deal with an older browser (looking at you, IE), include this polyfill:
Give a hoot, don't pollute... with unsafe integers
JavaScript has a maximum safe integer (Number.MAX_SAFE_INTEGER
). Going beyond that, precision takes a walk off a cliff leading to incorrect type checks—so keep it safe, folks!
Employing bitwise property
Bitwise operations can be used to verify integers. However, this superhero is not without a kryptonite—it falters beyond 32-bit integer range:
parseInt()
and equality
The parseInt()
function along with the mighty equality operator can pinpoint integers:
Watch out for false positives, though. parseInt
can turn strings to integers, making "123abc"
look like 123
.
Integer string check with regex
Regex saves the day once again when dealing with integer strings:
Precautions to take
Being aware of JavaScript's quirks can save hours of debugging and keep you sane:
- Is it finite? Just because
isFinite()
is old doesn't mean it's useless. You'll want to check finiteness before numeric type check. - Boundary check - think twice before flirting with JavaScript's
Number.MIN_VALUE
andNumber.MAX_SAFE_INTEGER
. - Negative integers - Yes, they're a thing. Check them right.
- Strings that look like numbers: Convert them to number before type checking. A monkey in a spacesuit is still a monkey.
Bonus: Checking complex number types
The world of numbers is packed with variety. Here's how to tackle numbers in different forms.
Exponential notation
A number like 1e2
is an integer. But, it's wearing a "cool guys don't look at explosions" sunglasses. Give it a uniform before checking:
Checking hexadecimal and binary
JavaScript treats strings starting with 0x
as hexadecimal and 0b
as binary. Be a good host and parse them first:
PHP parallels
If you're moonlighting as a PHP developer, the is_int()
, is_float()
, and ctype_digit()
functions should feel right at home.
References
Was this article helpful?