In javascript, parseInt ("08") and "09" return 0 problem analysis

Source: Internet
Author: User
Tags string format

Today, a bugger appears in the program. After debugging for a long time, the problem was discovered.

I did an experiment:

Alert (parseInt ("01"), when the value in this is 01 = 07, it is normal, but in "08 ", "09" will return 0

(This phenomenon occurs in ie kernel browsers, such as 360 browsers.) (Google and Firefox are not affected)
.

 

Check the information to find out the reasons for this phenomenon:

Explanation:


01--07 is normal, but and 08 are unqualified octal form, so it is processed as 0. To solve this problem, we can use another parameter of the parseInt function to tell parseInt to be processed in decimal format.
ParseInt ("08", 10) or parseInt ("09", 10)


ParseInt (string,
Radix) is used to convert a string to an integer. The second parameter is used to set the string format. Commonly used parameters include 2, 8, 10,
16 indicates the number of hexadecimal strings.

The value range of radix is 2 ~ 36. If it is not in this range, NaN is returned.


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

Starting with "0x", parseInt () treats the rest of string except 0x as a hexadecimal number,
Starting with "0", parseInt () treats the characters except 0 in string as octal or hexadecimal numbers,
Take 1 ~ Starting with a number of 9, parseInt () treats it as a decimal number.

 

Pay attention to it in js later !!!

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.