
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com
Mobile version of this pageSVN + ./todo + crontab
Next:
IssueTrackerProduct 0.7.2 released
Related blogs
Interesting float/int casting in PythonInteger division in programming languages
To assert or assertEqual in Python unit testing
Related by category
isInt() JavaScript function
assert, isdigit, isnan, tostring, parseint, isint, integer, int
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 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:
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
Am I allowed to use this function as long as I give you credit for it in my source code?
how about this:
String.prototype.isInt = function()
{
var re = new RegExp("^[\d]+$");
return this.match(re);
}
I tend to use:
function isInt (i) {
return (i % 1) == 0;
}


Save this page in del.icio.us
Thanx good idea i think. But are you sure the second part of the condition is useful?
Not sure:p
The second part is necessary so that "1.0" yields a false.
Indeed. My mistake:s