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″)

 

References:

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604

http://www.ventanazul.com/webzine/articles/issues-parseint-javascript

http://www.w3schools.com/jsref/jsref_parseint.asp

You must be logged in to post a comment.