DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Is Integer Value

07.04.2005
| 30923 views |
  • submit to reddit
        // Nota : Rafael Trindade da Silva - Mantus Deus Estrusco do Inferno
// Email : rafaeltds at gmail dot comfunction isInteger(s) {
  if (s == "") return;
	for (i = 0 ; i < s.length ; i++) {
  	if ((s.charAt(i) < '0') || (s.charAt(i) > '9'))	return false
	}
	return true;
}    

Comments

Nick Vacula replied on Tue, 2009/01/13 - 5:01am

@markos Same problem if parseInt("12345.0") or parseInt("12345.000000") comparing to "12345.000000". In that case parseInt(s).toString() == s will return false. simply try parseInt(s) == Number(s)

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

Nope, that one is wrong, because it should return true even for real numbers. parseInt just takes integer part out of a string. For example parseInt("12345.6") returns an integer of 12345. What you actually want is to check condition parseInt(s).toString() == s. It's true only when string s represents integer number.

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

Much better! Thanks a lot jonasraoni ...

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

isInteger = function( s ) { return !isNaN( parseInt( s ) ); }