How to Work Around JavaScript'S Parseint Octal Behavior

How do I work around JavaScript's parseInt octal behavior?

This is a common Javascript gotcha with a simple solution:

Just specify the base, or 'radix', like so:

parseInt('08',10); // 8

You could also use Number:

Number('08'); // 8

ParseInt() strange octal behavior

In this code, you can defined a number (rather than a string) in octal format, then passed it to parseInt. Then parseInt casts that number to a string ("83"), and parses it again.

If you pass a string to parseInt you will get the expected result:

console.log(parseInt('0123'))

Javascript ParseInt() with -1

parseInt("08") will give you NaN if the engine you're using extends parseInt to recognize octal (the leading 0 means "octal"). It will give you 8 if the engine follows the standard, which says not to do that (as of ES5).

parseInt("08"-1) is a bit redundant, because the expression "08"-1 will result in a number (which then gets passed into parseInt); just "08"-1 is all you need. Here's how that breaks down:

  • The engine processes the expression "08" - 1 by trying to turn "08" into a number. On an engine that extends numeric literal syntax to include octal, that would fail, but on engines that don't, it will give you 8. So the result of the expression is the number 7 (8 - 1).

  • The engine then does parseInt(7), which is redundant, as 7 is already a number. What happens is that 7 is turned into "7" and then parsed back into 7.

Note the difference above: The result of parseInt("08") - 1 will vary based on whether the engine extends what parseInt will parse, but the result of "08" - 1 will vary based on whether the engine extends numeric literal syntax. Those are two different things, and the ES5 specification does not allow engines to extend parseInt's syntax, but does allow them to extend numeric literal syntax (see §B.1.1), but only in loose mode. (Confusing, I know.)

The take-away message: Always use a radix with parseInt to tell it what number base to use.

In javascript: why does parseInt( 08 ) evaluate to zero, but parseInt(08) evaluate fine?

The solution here is simple. NEVER call parseInt() without specifying the desired radix. When you don't pass that second parameter, parseInt() tries to guess what the radix is based on the format of the number. When it guesses, it often gets it wrong.

Specify the radix like this and you will get the desired result:

parseInt("08", 10) == 8;

As to what rules it uses for guessing, you can refer to the MDN doc page for parseInt().

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.

So, according to these rules, parseInt() will guess that "08" is octal, but then it encounters a digit that isn't allowed in octal so it returns 0.

When you pass a number to parseInt(), it has nothing to do because the value is already a number so it doesn't try to change it.

How to represent an octal value as its string representation

The toString method for numbers has a radix parameter.

0755.toString(8)

Will return:

"755"

You can add the zero manually. For an example stringified function:

function stringified( number ) {
return "0" + number.toString(8);
}

Javascript, why treated as octal

If you receive your parameters as string objects, it should work to use

 parseInt(string, 10)

to interpret strings as decimal, even if they are beginning with 0.

In your test, you pass the parseInt method a number, not a string, maybe that's why it doesn't return the expected result.

Try

 parseInt('0000022115', 10)

instead of

parseInt(0000022115, 10)

that does return 221115 for me.

parseInt( 08 ) returns 0

That's because numbers started with 0 are considered to be octal. And 08 is a wrong number in octal.

Use parseInt('09', 10); instead.



Related Topics



Leave a reply



Submit