Analysis of parseInt functions in js

Source: Internet
Author: User

First, start with the popular instance parseInt ("09") = 0.
If the parseInt (number, type) function is followed by 2nd parameters, the default value is decimal.
For example, parseInt ("010", 10) is the result of the 10 hexadecimal notation: 10,
ParseInt ("010", 2) is the result of binary: 2,
ParseInt ("010", 8) is the result of octal: 8,
ParseInt ("010", 16) is the result of binary: 16.
 
The default value of the hexadecimal unit is 10 by default. However, if the Number in the system starts with 0, it is considered to be 8-hexadecimal, if it starts with 0x, it is regarded as hexadecimal. Www.2cto.com
ParseInt ("10") ==> parseInt ("010", 10) ==> 10 parseInt ("010") ==> parseInt ("010", 8) => 8 parseInt ("0x10") => parseInt ("010", 16) => 16.
 
It went quite well here.
Many things are not as smooth as possible. For example, if the Number is not a normal Number, how can we explain it. I checked some information and said this:
ParseInt (Number) if the Number in the middle is not a Number (0-9), the next Parsing is interrupted, and only the value before the Number is used for calculation: for example, parseInt ("100x") is equivalent to parseInt ("100") = "parseInt (" 100 ", 10) => 100, another example is parseInt ("0100x") ==> parseInt ("0100") ==> parseInt ("100", 8) ==> 64.
At this point, we seem to have ignored the existence of x in hexadecimal notation, which is not a number, and whether x is ignored. I tried to find that in this special process,
For example, parseInt ("0x10") ==> parseInt ("10", 16) ==> 16 is consistent with the top result, but it is consistent with what I just said, if it is not a character, it is automatically not parsed and should be understood as parseInt ("0") is against.
 
Here, we need to note that the Number in parseInt is like this. If the first character is not 0 and is not a character, it is regarded as a 10-digit system. If the first character is still executed, the letter stops. In addition, if the first one is 0, then you need to pay attention to it. If it is x, continue to go down. Here, x does not represent a value, it can only be combined with the preceding 0 to form a 0x representing the hexadecimal unit. The latter is based on the rules we mentioned above that non-numbers are used to stop parsing. For example, parseInt ("0xt") => parseInt ("0x") => parseInt ("", 16) is NaN.
 
To sum up, the value in parseInt (Number) is: if the first digit is not 0, it will stop parsing the letter, and the value before the letter will be parsed as a 10-digit system, if the first character is a letter, the value is null and the value is NaN,
For example, parseInt ("a") ==> parseInt ("", 10) ==> NaN. parseInt ("10a") ==> parseInt ("10") ==> parseInt ("10", 10) ==> 10;
If the first digit is 0 and the second digit is not x, the resolution will be stopped when the first letter is the same as the above, and the value before the letter will be parsed as an octal value. For example: parseInt ("0a") ==> parseInt ("0") ==> parseInt ("0", 10) ==> 0.PS: this is a bit special, because 0a is parsed to 0 and cannot be considered as an octal structure, the following one is obvious.
ParseInt ("010a") ==> parseInt ("010") ==> parseInt ("10", 8) ==> 8; if the first digit is 0, in addition, if the first character is "x", the system will stop parsing the letter and use the value in front of the letter as a hexadecimal parsing. For example, parseInt ("0xt ") ==> parseInt ("", 16) ==> NaN. parseInt ("0x12 t") ==> parseInt (" 12 ", 16) ==> 18

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.