Is parseInt () supposed to work like this?

If I write this script:

alert(parseInt("123blahblahblah456"));

I get a warning with a value of 123

Ideally, should a function NOT do something, as this is an invalid whole line? Similar to parseFloat()

+5
source share
7 answers

Yes: parseInt() absolutely designed to work; To specify an entry on the Mozilla Developer Network:

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the return value will be a decimal integer representation of the first argument, taken as the number in the specified base. For example, a radius of 10 indicates a conversion from decimal, 8 octal, 16 hexadecimal, etc. For radii above 10, letters of the alphabet indicate numbers greater than 9. For example, for hexadecimal numbers (base 16), A to F are used.

If parseInt encounters a character that is not a digit in the specified radius, it ignores it and all subsequent characters and returns an integer value processed to this point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

It seems that parseInt() explicitly expects to take a string and accept the first sequence of numbers (until it encounters an invalid numeric character) and returns that the radix parameter was specified as the number of some base.

By the way, in order to reduce errors when analyzing strings passed to parseInt() , do not forget to use the radix parameter, for example:

 parseInt('123odod24',10); // for base-10 parseInt('123odod24',16); // for base-16 

Link:

+10
source

Yes, all the answers. I would like to add that this is why to check whether it is possible to convert a certain value to a number, it is 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 

Keep in mind that this Number in some cases (optional) returns 0 .

 +null //=> 0 +" " //=> 0 +"" //=> 0 +false //=> 0 +[] //=> 0 
+13
source

parseInt tries to parse the string until it finds a non-integer value, after which it will return everything it had.

So if the line:

  • 1234abcd - it returns 1234
  • 1a3f - returns 1
  • a14883 - returns NaN
  • 1.5 - returns 1
  • -1.3a - returns -1

Same thing with parseFloat , except it won't break into .

  • 1234abcd - it returns 1234
  • 1a3f - returns 1
  • a14883 - returns NaN
  • 1.5 - it returns 1.5
  • -1.3a - returns -1.3
+11
source

Here's how it should work. It parses the string until it reaches a numeric character.

You may be interested in checking the isFinite() function; it checks if the string is a final, legal number:

 isFinite("123"); // true isFinite("123a");// false 

However, this will return true for empty lines and spaces. Therefore, you can 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 that you can create your own function that will ignore any string containing odd characters.

+5
source

As far as I know, the parseInt() method tries to parse until it finds a character. Therefore, if you have

  • parseInt("123 Iam funny") it will return 123.
  • parseInt("whats up 4711") it will return NaN

In some docs you can check:

+2
source

It is documented this way :

If parseInt encounters a character that is not a digit in the specified base, it ignores it and all subsequent characters and returns an integer value processed to this point.

Whether this behavior is a good idea is another matter, but it is too late to change it now.

+2
source

Yes! as explained here: http://www.w3schools.com/jsref/jsref_parseInt.asp

Tips and notes

Note: only the first number in the string is returned!

Note: Valid spaces and trailing spaces are allowed.

Note. If the first character cannot be converted to a number, parseInt () returns NaN.

Example

Parse different lines:

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

Code output above:

 10 10 34 60 40 NaN 10 8 8 16 16 
-one
source

Source: https://habr.com/ru/post/1398202/


All Articles