parseint in JavaScript ("08") and "09" Return 0 reason Analysis and solution _javascript tips

Source: Internet
Author: User

Today in the program appeared a bugger, debugging for a long time, finally found that the original is this problem.

An experiment was done:

Alert (parseint ("01")), when the value inside this is 01==== 07 o'clock is normal, but in "08", "09" will return 0

(this phenomenon appears in IE kernel browser, such as 360 browser will appear this error) (Google, Firefox is not affected)

Access to information on the causes of this phenomenon:

The interpretation of the great God:

01--07 Natural no problem, but 09,08 are unqualified octal form, so was treated according to 0. To solve this problem, you can take advantage of another parameter of the parseint function, which is shown to tell parseint to be processed in decimal.

parseint ("08", 10) or parseint ("09", 10)

The function of parseint (string,radix) is to convert a string to an integer, and the second argument is to format a string, with 2, 8, 10, and 16 commonly used, indicating how many numbers the string is.

The range of radix desirable values is 2~36, which, if not within this range, will return Nan.

If you set the value of radix to 0, or if you do not set it, the format of string is automatically recognized:

Beginning with "0x", parseint () takes the remainder of the string except 0x as a hexadecimal number.

Beginning with "0", parseint () treats a string except 0 as a octal or hexadecimal number.

Starting with a number 1 ~ 9, parseint () treats it as a decimal number.

The above is a small set to introduce the JavaScript parseint ("08") and "09" Return 0 reason Analysis and solution of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.