JRS: In article <cs*******************@news.demon.co.uk>, dated Sun, 16
Jan 2005 16:21:35, seen in news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted :
Evertjan. wrote:
<snip> function isInteger(n) {
return (!isNaN(n)) && (Math.floor(n)==n)
}
<snip>
As NaN is specified as never equalling NaN and Math.floor(NaN) results
in NaN:-
function isInteger(n){
return (Math.floor(n) == n);
}
- would be simpler.
isInteger("100000000000000000000000000000000000000 0000000.3")
isInteger("9007199254740993")
isInteger("3e+333")
isInteger("3e-333")
isInteger("0xbead")
give true.
Before testing for integer, one should first consider what one really
should be testing for; usually, one wants a digit in [1-9] followed by
not too many decimal digits. The OP should use a RegExp.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.