0

In my last question, I discovered that a number with a leading 0 is parsed as an octal number. In this question, the answer says:

This is a common Javascript gotcha with a simple solution:

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

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

That works, but only if the number is enclosed in quotes. Consider the following example:

var test = 0132;
alert(parseInt(test, 10));

JSFiddle: http://jsfiddle.net/VU96M/2/

The output of that is "90," not "132."

So, as you can see, the solution does not work int this case.

If I have a variables passed to a function as a parameter, or returned from a function, that has a leading zero like the above variable test, how can I parse it as a base 10 value?

Community
  • 1
  • 1
Nate
  • 26,164
  • 34
  • 130
  • 214
  • 2
    Well, for one: `parseInt(test), 10); != parseInt(test, 10);` – Mike G Apr 01 '13 at 18:29
  • 2
    `0132` is parsed as an octal number as soon as it's encountered, before `parseInt` is even called... – DCoder Apr 01 '13 at 18:30
  • @mikeTheLiar - A type on my part, I updated the question. – Nate Apr 01 '13 at 18:31
  • @DCoder - So, if the value is returned from a function, or is passed as an argument, how do I get it in base 10? – Nate Apr 01 '13 at 18:32
  • Your code shows a numeric literal but you are talking about a variable passed as a parameter or returned from a function. Can you show that code? It is surely easy to avoid in a literal by just not prepending with leading zeroes. – Martin Smith Apr 01 '13 at 18:33
  • 1
    If you're returning numbers from a function with a leading 0, then you are returning octal numbers. If that's not what you want, then change your function. – NilsH Apr 01 '13 at 18:34
  • 2
    A number is a number is a number. `0132` (octal) is the same number as `90` (decimal) is the same as `0x5A` (hex). A *string representation of the number* differs according to the base, the value of the number itself doesn't. Don't create number literals with leading zeroes, convert all strings to numbers using `parseInt(v, 10)`, and be done with it. – DCoder Apr 01 '13 at 18:36

3 Answers3

2

0132 is treated as an octal before it is passed to parseInt, so you are really calling parseInt(90,10)

Try this instead:

var test = 0132;
parseInt((test).toString(8)); // 132
lostsource
  • 21,070
  • 8
  • 66
  • 88
2

In strict mode JavaScript doesn't allow you to do such things – define an octal literal – and you will get an exception instead. It's really a good practice start to use the strict mode as default mode for your JavaScript code.

Also because in non strict mode you don't have a clue if test variable was defined as octal literal or decimal, so test.toString(8) will give you of course different results if test was defined as 0132 or 132.

ZER0
  • 24,846
  • 5
  • 51
  • 54
1

You can change it back to a base 8 literal so it will return the base 10 version:

console.log( +test.toString(8) ); // 132
//                 ^^^^^^^^^^^

Demo

David G
  • 94,763
  • 41
  • 167
  • 253