JavaScript: parseInt() bug with "08" and "09"

When calling parseInt(“08″) or parseInt(“09″) in browsers, it will return 0 instead of 8 and 9.

It is because it treats the numbers (with 0 in front) as octal. Since octal don’t have “08″ and “09″, so that it will return 0 instead.

The solutions will be:

(1) don’t add the zero before the number, i.e. parseInt(“8″) and parseInt(“9″)

(2) specify the radix, i.e. parseInt(“08″, 10) and parseInt(“09″, 10)

(3) use parseFloat instead of parseInt, i.e. parseFloat (“08″) and parseFloat (“09″)



You must be logged in to post a comment.