Skip to content Skip to sidebar Skip to footer

Javascript Function Parseint() Doesn't Parse Numbers With Leading 0 Correctly

I have some zeros prior to a positive integer. I want to remove the zeros so only the positive integer remains. Like '001' will only be '1'. I thought the easiest way was to use pa

Solution 1:

You have to specify the base of the number (radix)

parseInt('01', 10);

Solution 2:

This is documented behavior: http://www.w3schools.com/jsref/jsref_parseInt.asp

Strings with a leading '0' are parsed as if they were octal.

Solution 3:

Solution 4:

This is not actually a bug. For legacy reasons strings starting with 0 are interpreted in octal, and in octal there is no digit 8. To work around this you should explicitly pass a radix (i.e. parseInt("008", 10)).

Post a Comment for "Javascript Function Parseint() Doesn't Parse Numbers With Leading 0 Correctly"