How To Check If A Value Is An Integer In Javascript (special Case 1.0 Should Be Float)
I am writing some type check functions. I want: isInteger isFloat While writing isInteger, I noticed that isInteger(1.0) returns true. I want this to return false. My function is
Solution 1:
If you type
var a = 1;
var b = 1.0;
a
and b
are stored exactly the same way. There's no difference. That's because there's only one storage type for numbers in JavaScript, it's IEEE754 double precision floating point.
If you want to keep them different, don't use the number format, you may keep them as strings for example.
Solution 2:
try this way
check for a remainder when dividing by 1:
functionisInt(n) {
return n % 1 === 0;
}
Post a Comment for "How To Check If A Value Is An Integer In Javascript (special Case 1.0 Should Be Float)"