JavaScript parseint Function Analysis (RPM) _ Basics
Source: Internet
Author: User
The
parseint function of JavaScript
The parseint function of JavaScript, we all know what it is.
But you know
How much does parseint ("07") return?
How much is parseint ("08") returned?
The correct answer is
parseint ("07") returns 8
parseint ("08") returns 0
You know what the problem is?
In fact, this question may not have been thought of.
When using JavaScript's parseint function,
parseint ("08") or parseint ("09") returned incredibly 0,
and parseint ("a") ... parseint ("07") are correct.
Why do you do that?
It was hard to understand at first, and then the reason for this problem was that when there was a "0" in front,
The debugger of JavaScript would think it was a octal number,
and "08" and "09" are not a legitimate number of octal,
That led to the problem, but Parsefloat would not have the problem.
The parseint () function in the JavaScript reference is so described:
parseint method
Returns an integer converted from a string. Converts a string to an integral type.
parseint (numstring, [radix])
parseint parameter radix:
Options available. The value between 2 and 36 that represents the numstring of the saved number. If not provided, a string prefixed with ' 0x ' is treated as hexadecimal, and a string prefixed with ' 0 ' is treated as a octal. All other strings are treated as decimal.
So if you use parseint ("08", 10) or parseint ("09", 10),
Notifies the JavaScript interpreter that you can get the number you want by using decimal to parse.
Report:
parseint ("abc")//Return NaN.
parseint ("12ABC")//return 12.
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