Cause Analysis and Solution for zero returned by ParseInt ("08") and "09" in JavaScript, javascriptparseint
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)The function is to convert the string to an integer. The second parameter is to set the string format. Commonly Used values include 2, 8, 10, and 16, which indicate 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.
The above section describes the Cause Analysis and Solution knowledge of ParseInt ("08") and "09" Returned 0 in JavaScript. I hope it will be helpful to you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!