JavaScript parseint function __ function

Source: Internet
Author: User
<script>
var b = parseint ("a");
Alert ("b=" +b); 1
var c = parseint ("09/08/2009");
Alert ("c=" +c); 0
</script>

The answer is b=1,c=0.

parseint () is used to parse the string, and the return value is an integer. There are several features:


1, ignoring the first and last spaces of the parsed string, that is: "111" and "111" are the same

2), formal format is parseint (String,radix). Radix can be omitted, as an integer between 0 or 2-36, to denote the binary of the parsed value. (Note that it is not a system that resolves the returned results.) If Radix is not within this range, return Nan.

3), if the radix is omitted or 0, the string is parsed by the default system. The first character of string is 0, the default resolution is 8. The string with the first character of 0x is resolved to 16 by default. In other cases, the default is 10. 4, parsing from the first can be resolved from the beginning of the character, to the first unresolved characters (such as spaces, punctuation, etc.) end. The characters that follow are no longer parsed. Returns Nan if the first character cannot be resolved. such as "a8989"


Now we can look back at this problem is easy to understand:


The first letter of B is 0, and the following value is parsed by 8, which is 1 of 8. The natural return value is 1.

The difficulty lies in the second one.


The first character of C is 0, and the general idea is to parse it by 8. But we found that the second character, 9, is not a 8-digit number, which means that 9 is the first character that cannot be resolved. parseint ("09/08/2009") became a parseint ("0"), and the result was more pronounced, 0.


If we slightly change the parseint ("0119/08/2009"), the first character is 0, the octal parsing, the same reading to 9 can not parse. It becomes parseint ("011"), and the result is obviously 9.

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.