Reputation: 2813
If I write this script :
alert(parseInt("123blahblahblah456"));
I get the alert with the value 123
Ideally, shouldn't the function NOT do anything since it is an invalid integer string?
Similar is the case with parseFloat()
Upvotes: 17
Views: 13056
Reputation: 122898
Yes, cf all the anwers. I'd like to add that this is why checking if certain value can be converted to a number, it's better to use Number
or just +
.
Number("123blahblahblah456"); //=> NaN
Number("123"); //=> 123
+"97.221" //=> 97.221
// if the conversion result needs to be an int
Math.round(Number("123.4567")); //=> 123
Be aware though that Number
in some cases (unexpectely) returns 0
.
+null //=> 0
+" " //=> 0
+"" //=> 0
+false //=> 0
+[] //=> 0
Upvotes: 21
Reputation: 7310
This is how it is suppose to work. It parse the string until it reaches a non numerical character.
You might be interested in checking out the function isFinite()
, it checks wether a string is a finite, legal number:
isFinite("123"); // true
isFinite("123a");// false
However this will return true for empty string and whitespace. You can therefore improve this solution by writing
mystring = "123";
mystringb = " ";
!isNaN(parseInt(mystring)) && isFinite(mystring); // true
!isNaN(parseInt(mystringb)) && isFinite(mystringb); // false
Based on these suggestions I'm sure you can build your own function that will ignore any string that contains nonnumerical characters.
Upvotes: 7
Reputation: 269318
It's documented to behave like that:
If
parseInt
encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point.
Whether that behaviour is a good idea is another matter, but it's too late to change it now.
Upvotes: 3
Reputation: 7536
As far as I know the parseInt()
method is trying to parse until it finds a character. So if you have
parseInt("123 Iam funny")
it will return 123.parseInt("whats up 4711")
it will return NaNSome documentation you might be checking out :
Upvotes: 2
Reputation: 8183
yes ! like explain here : http://www.w3schools.com/jsref/jsref_parseInt.asp
Tips and Notes
Note: Only the first number in the string is returned!
Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number, parseInt() returns NaN.
Example
Parse different strings:
<script type="text/javascript">
document.write(parseInt("10") + "<br />");
document.write(parseInt("10.33") + "<br />");
document.write(parseInt("34 45 66") + "<br />");
document.write(parseInt(" 60 ") + "<br />");
document.write(parseInt("40 years") + "<br />");
document.write(parseInt("He was 40") + "<br />");
document.write("<br />");
document.write(parseInt("10",10)+ "<br />");
document.write(parseInt("010")+ "<br />");
document.write(parseInt("10",8)+ "<br />");
document.write(parseInt("0x10")+ "<br />");
document.write(parseInt("10",16)+ "<br />");
</script>
The output of the code above will be:
10
10
34
60
40
NaN
10
8
8
16
16
Upvotes: -2
Reputation: 253308
Yes: parseInt()
is absolutely meant to work like that; to quote the Mozilla Developer Network entry:
The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
It seems that parseInt()
is explicitly expecting to take a string and will take the first sequence of numbers (until it encounters an invalid numerical character) and return that as a number of whatever base was specified in the radix parameter.
Incidentally, to reduce errors when parsing the strings passed to parseInt()
remember to use the radix parameter, for example:
parseInt('123odod24',10); // for base-10
parseInt('123odod24',16); // for base-16
Reference:
Upvotes: 17
Reputation: 15802
parseInt
attempts to parse the string until it finds a non-integer value, at which point it returns whatever it had.
So if the string is:
Same with parseFloat
except that won't break on a .
Upvotes: 13