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 you8
. So the result of the expression is the number 7 (8 - 1).The engine then does
parseInt(7)
, which is redundant, as7
is already a number. What happens is that7
is turned into"7"
and then parsed back into7
.
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
Get Selected Text from a Drop-Down List (Select Box) Using Jquery
How to Check For an Empty/Undefined/Null String in JavaScript
How to Find Out the Caller Function in JavaScript
Pass in an Array of Deferreds to $.When()
How to Get Started With Node.Js
How to Measure Time Taken by a Function to Execute
++Somevariable Vs. Somevariable++ in JavaScript
What Are the Precise Semantics of Block-Level Functions in Es6
What Are These Three Dots in React Doing
How to Use Arrow Functions (Public Class Fields) as Class Methods
What's the Best Way to Detect a 'Touch Screen' Device Using JavaScript
How to Detect the Version of a Browser
How to Get Function Parameter Names/Values Dynamically
Can Someone Explain the "Debounce" Function in JavaScript
How to Distinguish Between Left and Right Mouse Click With Jquery
Why Doesn't This Arrow Function Work in Ie 11