The use of parseint functions in JavaScript

Source: Internet
Author: User

javascript parseint function , everybody knows what it is.
but you know what
parseint ("07") returns?
parseint ("08") and return how much?

The correct answer is
parseint ("07") returns 8
parseint ("08") returns 0

you know where the problem is?
In fact, this question may not have been thought of by everyone.

When using JavaScript's parseint function,
parseint ("08") or parseint ("09") returned is actually 0,
and parseint ("" ") ... parseint (" 07 ") are all right.

Why is this?
was difficult to understand at first, and later found that the problem was that when "0" was in front,
JavaScript's debugger would think of it as an octal number
and "08" and "09" were not a valid octal number,
This causes the problem, but parsefloat does not exist. The parseint () function in the

JavaScript Reference is described as follows:

parseint Method
Returns an integer converted from a string. Converts a string to an integer type.

parseint (numstring, [radix])

parseint parameters Radix :

available options. The value between 2 and 36 represents the numstring of the saved number. If not provided, the string prefixed with ' 0x ' is treated as hexadecimal, and the string prefixed with ' 0 ' is treated as an octal. All other strings are considered to be decimal.

Therefore, if you use parseint ("08", 10) or parseint ("$"),
notifies the JavaScript interpreter to use decimal to parse to get the number you want.

Attached:
parseint ("abc")     //Return NaN.

parseint ("12ABC")//return 12.

As shown in the picture Carousel, when clicking on the image Number link, the innerHTML value in the link is converted to decimal integer number.

The use of parseint functions in JavaScript

Related Article

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.