Parsing the parseint function 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 10 hexadecimal: 10, parseint ("010", 2) is the result of 2 hexadecimal: 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.

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 I had processed 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 values in parseint (number) are as follows:

If the first character is not 0, it will stop parsing the letter and use the value in front of the letter as the 10-digit System for parsing. 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 regarded as an octal structure. The one below is obvious.

Parseint ("010a") ==> parseint ("010") ==> parseint ("10", 8) ==> 8;

If the first digit is 0 and the second digit is X, the resolution will be stopped when you encounter a letter like the above, and the value in front of the letter will be parsed as hexadecimal. For example:

Parseint ("0xt") ==> parseint ("", 16) ==> Nan.

Parseint ("0x12 t") ==> parseint (" 12 ", 16) ==> 18.

PS:

When our parseint is a number, the interpretation is different. If our parameter is set to 090, instead of 0, 90 is returned.

Ex:

Parseint (070) = 56

Parseint (090) = 90

When it is 0, it is in hexadecimal format, but if it is greater than 7, it is in hexadecimal format.

If it starts with 0x, you do not need to consider whether it is followed by a letter, because if it is a letter, an error will be reported, ^ _ ^

 

 

 

 

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.