Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is parseInt() supposed to work like this?

Tags:

javascript

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()

like image 432
Vrushank Avatar asked Feb 24 '12 10:02

Vrushank


People also ask

How does parseInt work?

The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .

When parseInt () method can be used?

Java - parseInt() Method This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.

What is parseInt () in Java?

The method parseInt() belongs to the integer class which is under java. lang package . It is used to parse the string value as a decimal value that is signed. It is used in java for typed conversion for converting a string value to an integer by using the method parseInt().

Is it better to use number or parseInt?

Number() converts the type whereas parseInt parses the value of input. As you see, parseInt will parse up to the first non-digit character. On the other hand, Number will try to convert the entire string.


2 Answers

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:

  • parseInt() at the MDC.
like image 99
David Thomas Avatar answered Oct 04 '22 21:10

David Thomas


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
like image 31
KooiInc Avatar answered Oct 04 '22 21:10

KooiInc