Javascript parseint Function Analysis)

Source: Internet
Author: User

Javascript ParseintFunction
We all know what the Javascript parseint function is.
But you know
What is returned by parseint ("07?
What is returned by parseint ("08?
The correct answer is:
Parseint ("07") returns 8
Parseint ("08") returns 0
You know where the problem is?
In fact, you may not have thought about this issue.
When using the Javascript parseint function,
Parseint ("08") or parseint ("09") returns 0,
Parseint ("01")... parseint ("07") is correct.
Why?
It was hard to understand at first, and later I found that the cause of this problem was that when there was "0" in front,
The debugger of JavaScript considers this to be an octal number,
"08" and "09" are not legal Octal numbers,
This problem occurs, but parsefloat does not.
The parseint () function in Javascript reference is described as follows:
Parseint Method
Returns an integer converted from a string. Converts a string to an integer.
Parseint (numstring, [Radix])
The parseint parameter Radix:
Optional. Between 2 and 36, it indicates the hexadecimal value of the number saved by numstring. If this parameter is not provided, the string prefixed with '0x 'is treated as hexadecimal, and the string prefixed with '0' is treated as octal. All other strings are treated as decimal.
Therefore, if you use parseint ("08", 10) or parseint ("09", 10 ),
Notify the javascript interpreter to use decimal to parse and obtain the number you want.
Appendix:
Parseint ("ABC") // return Nan.
Parseint ("12abc") // return 12.

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.