A brief summary of the trap analysis of JS parseint _javascript Skills

Source: Internet
Author: User
Copy Code code as follows:

var a = parseint ("a"), B = Number ("09");

A lot of people think that A and B are numbers 9, but they're not.

The main function of parseint is to convert a string to an integer, or to convert a decimal number to an integer. In general, we use only its first argument. But in fact, it has two parameters:
parseint (string, radix)

The parseint will be converted according to the radix specified, for example:
Copy Code code as follows:

Alert (parseint ("10", 2)); Outputs ' 2 '

In the case where radix is not specified or radix is 0, parseint is converted in decimal. However, this is somewhat special in some cases:

* If the value of string starts with "0x", the parseint is converted in hexadecimal;
* If the value of string starts with "0", the parseint is converted by octal.

Back to the beginning of the code, because "09" is started with "0", so parseint will be converted by octal, but "9" is not a valid octal value (octal only 0-7 eight digits), so the conversion result is 0.

To avoid this trap, you can force the specified radix:
Copy Code code as follows:

Alert (parseint ("09", 10)); Outputs ' 9 '

Other Netizen's supplement:
Look at the code:
Copy Code code as follows:

Alert (parseint (0.000001));
Alert (parseint (0.0000001));

The first statement outputs 0, and the second statement outputs 1, which is embarrassing.

Keep looking at the code:
Copy Code code as follows:

Alert (parseint (' 0.000001 '));
Alert (parseint (' 0.0000001 '));

Output 0, which is in line with expectations.

View the ECMA-262 specification, parseint calls the ToString method first. The problem has gradually become clear:
Copy Code code as follows:

alert (0.000001);
alert (0.0000001);

The first statement is output as is, and the second statement outputs 1e-7.
Continue to search the ECMA-262 9.8.1 ToString applied to the number Type, which suddenly dawned:
Copy Code code as follows:

Assertequals ("0.00001", (0.00001). toString ());
Assertequals ("0.000001", (0.000001). toString ());
Assertequals ("1e-7", (0.0000001). toString ());
Assertequals ("1.2e-7", (0.00000012). toString ());
Assertequals ("1.23e-7", (0.000000123). toString ());
Assertequals ("1e-8", (0.00000001). toString ());
Assertequals ("1.2e-8", (0.000000012). toString ());

Above is the unit test script for the V8 engine number-tostring, which is a good illustration of the ECMA specification.

Summary: For values less than 1e-6, ToString is automatically converted to scientific notation. So the parseint method, when the parameter type is uncertain, is best to encapsulate one layer:
Copy Code code as follows:

function ParseInt2 (a) {
if (typeof a = = = ' number ') {
Return Math.floor (a);
}
Return parseint (a);
}

Blueidea the above article:
Something that was done last November, and by the Year 8, September suddenly went wrong
Debugged for 1x minutes and finally found out it was Parseint's problem.

Instructions in MSDN
Description
Converts strings into integers.
Syntax
parseint (numstring, [radix])
The parseint method syntax has these parts:
Part Description
[Numstring] Required. A string to convert into a number.
[Radix] Optional. A value between 2 and indicating the base of the number contained in numstring. If not supplied, strings with a prefix of ' 0x ' are considered hexidecimal and strings with a prefix of ' 0 ' are Octal. All other strings are considered decimal.

Use the parseint is on the date, for example 2005-10-08 uses regular to solve the month day string, then uses parseint to solve the number to use.
<script language= "JScript" >alert (parseint ("") </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

<script language= "JScript" >alert (parseint ("a") </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Is it different from what you see?
The "08" string is recognized as 8, parseint ("08") comes out of 0 because 8 is not 8
General use parseint also will not deliberately to write behind the radix, the default is the decimal
Now it seems that everyone is still hard-working, write a 10 more, just insurance ah

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.