patternjavascriptMinor
Simple function to verify if a number is integer
Viewed 0 times
numbersimplefunctionverifyinteger
Problem
Sometimes, there's the need to verify if a given number is an integer or a decimal. Since JavaScript doesn't distinguish between both, I've made an extremelly basic function.
As you can see, it relies on checking if the number without decimals is the same as the parsed value. I'm not so confident about this.
Notice that this may return
Examples of results:
Is there any more reliable way of checking? The results seem to be consistent with the expected results. Regular expressions are an option but they are clunky.
function isInteger(num){
var numCopy = parseFloat(num);
return !isNaN(numCopy) && numCopy == numCopy.toFixed();
}As you can see, it relies on checking if the number without decimals is the same as the parsed value. I'm not so confident about this.
Notice that this may return
true for some scientific notation values.Examples of results:
1.0:true(JavaScript treats.0as an integer)
1e3:true
1.2345e2:false(equivalent to 123.45)
1.2345e5:true(equivalent to 123450)
1e200:true(equivalent toInfinity)
Is there any more reliable way of checking? The results seem to be consistent with the expected results. Regular expressions are an option but they are clunky.
Solution
In ES6, the next version of Javascript, there will be a function exactly for this purpose called
I think this is the best we will do in the meanwhile, as passing string to a number function is an effective way of doing it, but calling
There are already a couple of browsers that support it like Chrome and Firefox, but the big one (Safari) doesn't yet and neither does IE/Edge, so the polyfill will have to do. For more info, see MDN's writeup, it's quite insightful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Browser_compatibility
Note: Polyfills like this are useful, but there are some concerns about amending these core features of JS. As I say in the comments, as long as you implement something that doesn't exist or replace anything, I think it's alright. Use in moderation.
You can always wrap it in it's own function and call it as a global function if you prefer not to use this polyfill.
Number.isInteger(). Currently, MDN offers the following polyfill which I assume therefore does the best Javascript can to determine whether something is an integer. There are edge cases of course, but for most everyday use it will do the trick:Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};I think this is the best we will do in the meanwhile, as passing string to a number function is an effective way of doing it, but calling
toString on a number makes it go through conversion as well, resulting in potential issues.There are already a couple of browsers that support it like Chrome and Firefox, but the big one (Safari) doesn't yet and neither does IE/Edge, so the polyfill will have to do. For more info, see MDN's writeup, it's quite insightful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Browser_compatibility
Note: Polyfills like this are useful, but there are some concerns about amending these core features of JS. As I say in the comments, as long as you implement something that doesn't exist or replace anything, I think it's alright. Use in moderation.
You can always wrap it in it's own function and call it as a global function if you prefer not to use this polyfill.
Code Snippets
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};Context
StackExchange Code Review Q#101484, answer score: 5
Revisions (0)
No revisions yet.