isInt() JavaScript function

22 May 2006   23 comments   Web development

Powered by Fusion×

Here's my take on a function that determines if a variable is an integer or not before or after it's been recast to an int. The functionality is very similar to Python's 'isdigit()' function which works like this:

>>> assert "1".isdigit()
>>> assert not "1.0".isdigit()

However, my Javascript function should return true when the input is an integer or a string of an integer. Here it goes:

function isInt(x) {
   var y = parseInt(x, 10);
   return !isNaN(y) && x == y && x.toString() == y.toString();
}
assert(isInt("1"));
assert(isInt(1));
assert(!isInt("1a"));
assert(!isInt("1.0"));

You can see it in action here.
To be honest, I'm writing about this here just to not forget it the next time I need a similar function. Sorry to cause any interweb-noise.

UPDATE

Corrected whitespacing and made a jsFiddle link.

Comments

Manuel SIMONOT
Thanx good idea i think. But are you sure the second part of the condition is useful?

Not sure:p
Peter Bengtsson
The second part is necessary so that "1.0" yields a false.
Anonymous
Indeed. My mistake:s
Anonymous
d
Kal
Am I allowed to use this function as long as I give you credit for it in my source code?
Okonomiyaki3000
how about this:

String.prototype.isInt = function()
{
var re = new RegExp("^[\d]+$");
return this.match(re);
}
marchaos
that will fail for negative numbers.
Anonymous
assert -> alert
ankit
Thanks the original function worked for me.
scott christopher
I tend to use:

function isInt (i) {
return (i % 1) == 0;
}
Peter Bengtsson
This fails on floating point numbers. isInt("1.1") is false but isInt("1.0") is true which it's not supposed to.
Stuart Thiel
Thanks, used and credited.
onli
Thank you.
halbesbit
sorry i post the false code :/, delete it please.
isInt=function (i) { return ((i % 1) == 0)? i:false; }
Peter Bengtsson
Thanks. That's what scott suggested above. His approach returns only true or false.
msangel
function isInteger(s){
return (s%(parseInt(s)/Number(s)))===0;
}

is perfect solution (=
gonzoyumo
except when you want to consider "0" is an integer (which does)
Tex
Manuel SIMONOT feeling was right, there is useless parts. But he picked the really wrong one...

function isInt(n) {
return n.toString()==parseInt(n).toString();
}
// 'a' != 'NaN' so 'a' is not an Int
// '1.0' != '1' so '1.0' is not an Int

Peter want "1.0" to be false. So n%1==0 is the wrong test for that. But this request is, in a way, absurd. "1.0" is findable as being a Float. 1.0 can't. It is parsed from Float to Int before being send to the function. Displaying a 1.0 can only be done from a string. alert(1.0); give 1. And the test wrote in the article doesn't test Integer or Float, only String.
Aadit M Shah
The simplest form of isInt is as follows (for a number n):

parseInt(n) === n; //isInt

That's it - you don't need a function call! Similarly, we have the following:

parseInt(n) !== n; //isNotInt
parseFloat(n) === n; //isFloat
parseFloat(n) !== n; //isNotFloat
Anonymous
And how would you check this "03" ?
Bobinours
It depend of the version of EcmaScript and the browser.

"If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10)."

"The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values."

(Source : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt )

So... For this reason, you should always specify a radix when using parseInt().

parseInt("03", 10);
Anonymous
Why this:

var n = 3.0;
alert(parseInt(n)===n);

alerts "true"?
Peter Bengtsson
Python does the same:

>>> 3.0 == 3
True

But in python you can do:

>>> 3 is 3
True
>>> 3.0 is 3.0
True
>>> 3.0 is 3
False
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
SVN + ./todo + crontab 20 May 2006
Next:
IssueTrackerProduct 0.7.2 released 22 May 2006
Related by keywords:
Interesting float/int casting in Python 25 April 2006
To assert or assertEqual in Python unit testing 14 February 2009
Integer division in programming languages 04 August 2004