Kwissle

My real-time quiz battle game Kwissle.com

Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Photos

Photoalbum, both old and new.

Twitter

Follow me on Twitter

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 
Web development

isInt() JavaScript function


22nd of May 2006

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);
   if (isNaN(y)) return false;
   return 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.



Comment

22 comments so far
Manuel SIMONOT - 26th May 2006  [«« Reply to this]
Thanx good idea i think. But are you sure the second part of the condition is useful?

Not sure:p
Peter Bengtsson - 30th May 2006   [«« Reply to this]
The second part is necessary so that "1.0" yields a false.
Anonymous - 31st May 2006   [«« Reply to this]
Indeed. My mistake:s
Anonymous - 4th May 2011   [«« Reply to this]
d
Kal - 16th April 2007  [«« Reply to this]
Am I allowed to use this function as long as I give you credit for it in my source code?
Okonomiyaki3000 - 11th December 2007  [«« Reply to this]
how about this:

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

function isInt (i) {
return (i % 1) == 0;
}
Peter Bengtsson - 27th April 2010   [«« Reply to this]
This fails on floating point numbers. isInt("1.1") is false but isInt("1.0") is true which it's not supposed to.
onli - 9th February 2009  [«« Reply to this]
Thank you.
halbesbit - 26th April 2010  [«« Reply to this]
sorry i post the false code :/, delete it please.
isInt=function (i) { return ((i % 1) == 0)? i:false; }
Peter Bengtsson - 27th April 2010   [«« Reply to this]
Thanks. That's what scott suggested above. His approach returns only true or false.
msangel - 18th September 2010  [«« Reply to this]
function isInteger(s){
return (s%(parseInt(s)/Number(s)))===0;
}

is perfect solution (=
gonzoyumo - 28th January 2011   [«« Reply to this]
except when you want to consider "0" is an integer (which does)
Tex - 24th February 2011  [«« Reply to this]
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 - 24th July 2011  [«« Reply to this]
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 - 3rd November 2011   [«« Reply to this]
And how would you check this "03" ?
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.