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 parseInt('001'). But what I discovered is that it don't works for the number 8 and 9. Example parseInt('008') will result in '0' instead of '8'.

Here are the whole html code:

<html> <body>
var integer = parseInt('002');

</body> </html>

But can I somehow report this problem? Do anyone know an another easy workaround this problem?



This is documented behavior:

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


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

parseInt('01', 10);

Number prefixed with zero is parsed as octal.


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


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.