A bug appeared in the program today. I debugged it for a long time and finally found out that it was this problem.
A experiment was done:
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 browsers of the IKEA kernel, such as 360 browser, this error will occur) (Google, Firefox will not be affected)
.
I learned about the reasons for this phenomenon after reading the information:
The great god's explanation:
01--07 naturally has no problem, but 09 and 08 are all unqualified octal forms, so they were processed as 0. To solve this problem, you can use another parameter of the parseInt function to tell parseInt to process it in decimal.
parseInt("08",10) or parseInt("09",10)
parseInt(string, radix) is to convert string into integers. The second parameter is to set the format of string. Commonly used are 2, 8, 10, and 16, indicating the number of the string in what is the binary number.
The range of radix can be desirable is 2~36. If it is not within this range, NaN will be returned.
If the value of radix is set to be 0 or not, the format of string will be automatically recognized:
Starting with "0x", parseInt() will treat the rest of string except 0x as a hexadecimal number.
Starting with "0", parseInt() will treat characters other than 0 as octal or hexadecimal numbers.
Starting with numbers 1 to 9, parseInt() will treat it as a decimal number.
The above is the relevant knowledge about the reasons why ParseInt("08") and "09" return 0 in JavaScript introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to Wulin.com website!