Javascript parseInt() with leading zeros

Javascript

Javascript Problem Overview


Javascript's parseInt function does not seem to completely work.

parseInt("01") returns 1
parseInt("02") returns 2
parseInt("03") returns 3
parseInt("04") returns 4
parseInt("05") returns 5
parseInt("06") returns 6
parseInt("07") returns 7
parseInt("08") returns 0
parseInt("09") returns 0

You can't explain that. Give it a try. (jsFiddle)

Edit Since this question was asked and answered, the "feature" of defaulting to octal radix has been deprecated. [1] [2]

Javascript Solutions


Solution 1 - Javascript

This is because if a number starts with a '0', it's treated as base 8 (octal).

You can force the base by passing the base as the 2nd parameter.

parseInt("09", 10) // 9

According to the docs, the 2nd parameter is optional, but it's not always assumed to be 10, as you can see from your example.

Solution 2 - Javascript

Calls to parseInt should always specify a base in the second argument:

parseInt("08", 10);

Earlier versions of JavaScript treat strings starting with 0 as octal (when no base is specified) and neither 08 nor 09 are valid octal numbers.

From the Mozilla documentation: > > If radix is undefined or 0, JavaScript assumes the following: > > - If the input string begins with "0x" or "0X", radix is 16 > (hexadecimal). > - If the input string begins with "0", radix is eight > (octal). This feature is non-standard, and some implementations > deliberately do not support it (instead using the radix 10). For this > reason always specify a radix when using parseInt. > - If the input string > begins with any other value, the radix is 10 (decimal). > > If the first character cannot be converted to a number, parseInt returns NaN.

And from the ECMAScript 3 standard:

> When radix is 0 or undefined and the string's number begins with a 0 > digit not followed by an x or X, then the implementation may, at its > discretion, interpret the number either as being octal or as being > decimal. Implementations are encouraged to interpret numbers in this > case as being decimal.

The latest version of JavaScript (ECMAScript 5) abandons this behavior, but you should still specify the radix to satisfy older browsers.

Solution 3 - Javascript

There is a Radix parameter

parseInt(value, base)

Where base is the radix.

In this case you are evaluating base10 (decimal) numbers, therefore use

parseInt(value, 10);

Solution 4 - Javascript

This doesn't seem to be completely valid in new browsers. Internet Explorer 9 and 10 will return 8 if you execute 'parseInt("08")' whereas Internet Explorer 8 and previous versions will return 0 (IE10 in quirks mode will also return 0).

The latest version of Chrome also returns 8 so they must have changed the interpretation recently.

Solution 5 - Javascript

This issue is deprecated now. But you can still use radix in parseInt to convert number of other bases into base-10. E.g.,

var baseTwoNumber = parseInt('0010001', 2);

returns 17 (which is base-10 of 0010001).

Solution 6 - Javascript

Tip: As you now know when default to octal has been deprecated. Here is how you would fix it in legacy browsers

// ES-5 15.1.2.2
if (parseInt('08') !== 8 || parseInt('0x16') !== 22) {
    parseInt = (function (origParseInt) {
        var hexRegex = /^0[xX]/;
        return function parseIntES5(str, radix) {
            str = String(str).trim();
            if (!Number(radix)) {
                radix = hexRegex.test(str) ? 16 : 10;
            }
            return origParseInt(str, radix);
        };
    }(parseInt));
}

Solution 7 - Javascript

The issue seems to have changed now in most browsers.

Firefox 51.0.1 (64-bit)

parseInt("09")   // 9

Chrome 55.0.2883.95 (64-bit)

parseInt("09")   // 9

Safari 10.0 (12602.1.50.0.10)

parseInt("09")   // 9

=====

Having said that, just to be on the safer side and to avoid issues, use the base/radix parameter as suggested in the accepted answer.

parseInt("09", 10)    // 9

Extra test

I just wanted to test this as well, if the argument is not a string. Chrome & Safari gives exact result. Firefox too returns proper result, but with a warning.

parseInt(09)     // 9. (Warning: SyntaxError: 09 is not a legal ECMA-262 octal constant)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsavingerView Question on Stackoverflow
Solution 1 - JavascriptRocket HazmatView Answer on Stackoverflow
Solution 2 - JavascriptWayneView Answer on Stackoverflow
Solution 3 - JavascriptJohn HartsockView Answer on Stackoverflow
Solution 4 - JavascriptMaffeluView Answer on Stackoverflow
Solution 5 - JavascriptMuhammad ImranView Answer on Stackoverflow
Solution 6 - JavascriptEndlessView Answer on Stackoverflow
Solution 7 - JavascriptmythicalcoderView Answer on Stackoverflow