Understand JS's parseint (Turn)

Source: Internet
Author: User

The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns NaN, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.

First, the grammar

parseint (string, radix)

Parameters Describe
String Necessary. The string to be parsed.
Radix

Optional. Represents the cardinality of the number to parse. The value is between 2 and 36 or 0.

If this argument is omitted or its value is 0, the number is parsed based on 10. If it starts with "0x" or "0X", it will have a base of 16.

If the parameter is less than 2 and is not 0, or greater than 36, parseint () returns NaN.

Ii. Detailed rules for the application of the "string" parameter ( take the 10 binary example )

1, if all are letters, return: NaN

parseint ("abc", 10)//returns NaN 2, if all are numbers, returns an integer

parseint ("123", 10)///return 123

3. If both letters and numbers exist

(1), starting with a number, convert all numbers before the first letter appears

parseint ("12X2BC", 10)///return: 12

(2), if the parameter "string", start with a letter, directly return Nan (10 in the alphabet is not a valid representation)

parseint ("DF2BC", 10)//Return NaN

Third, use parseint () to parse different string instances

parseint ("ten");            Returns 10
parseint ("n", "ten");        Returns 19 (10+9)
parseint ("One", 2);        Returns 3 (2+1)
parseint ("n", 8);        Returns 15 (8+7)
parseint ("1f", +);        Returns 31 (16+15)
parseint ("010");        Undecided: Returns 10 or 8

Boring personal note how to calculate the System ~~~~~~ la La la ~~~~~
Here I use (n) [2] to represent 2 binary 2 binary with 2 for full bit, as in decimal 10 for full bit.
(17) [8]---> (1111) [2]---> (2 of the 3-square + 2 of 2-square + 2-square + 1-square) [2] = (8+4+2+1) [0] = (10) [10]

Note 16 binary after 10 with A instead of 1a==10+16=26------a=16 1=10
a1==10*16+1=161

Four, JS in the use of parseint need to pay attention to the place

1, in the absence of specifying radix or radix 0, parseint will be converted by decimal. However, if the value of string starts with "0x", the parseint is converted in hexadecimal;

2, parseint will call the ToString method, see the following several parseint return values

alert (0.000001); Pop Up 0.000001
alert (0.0000001); Pop Up 1e-7

parseint (0.000001)//Return 0

parseint (0.0000001)//return 1

parseint (' 0.000001 ')//return 0
parseint (' 0.0000001 ')//return 0

Skillfully used: parseint (X.style.marginleft, 10); this removes the PX from the back.

Ext.: http://www.studyofnet.com/news/941.html

Understand JS's parseint (Turn)

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.