5

Why does JavaScript convert parseInt(0000000101126) to 33366 instead of 101126?

var example = parseInt(0000000101126);
console.log(example); //33366 
Jonathan
  • 3,016
  • 9
  • 43
  • 74

7 Answers7

2

JavaScript assumes the following:

•If the string begins with "0x", the radix is 16 (hexadecimal)
•If the string begins with "0", the radix is 8 (octal). This feature is deprecated
•If the string begins with any other value, the radix is 10 (decimal)

Zzyrk
  • 907
  • 8
  • 16
1

Try putting your value in quotes it will give you proper output:

Instead of:

var example = parseInt(0000000101126);

Try

var example = parseInt("0000000101126");
Bhushan
  • 6,151
  • 13
  • 58
  • 91
  • I can't explicitly write parseInt("0000000101126") because 0000000101126 is coming from one row out of thousands being processed in a loop. Even if I put 0000000101126 into a variable first and convert it to string using example.toString() it still returns 33366 – Jonathan Jan 21 '14 at 13:43
  • 1
    @Jonathan You could post another question with the code that produces parseInt(010126) signaling the problem (that the number is interpreted as octal) and you can get a solution. Your current question seems to be limited to finding out why this correct behavior is happening, not how to get a solution to your problem. – Tibos Jan 21 '14 at 15:38
  • @DownVoter reason for downvoting will be appreciated. – Bhushan Jan 22 '14 at 05:25
0

if parseInt value starts from 0 javascript engine evaluate it like octal value. But ECMAScript 5 removes octal interpretation.

Alex
  • 11,115
  • 12
  • 51
  • 64
0

This is because it's being converted to Octal.

The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number. If the radix parameter is omitted, JavaScript assumes the following:

- If the string begins with "0x", the radix is 16 (hexadecimal)
- If the string begins with "0", the radix is 8 (octal). This feature is deprecated
- If the string begins with any other value, the radix is 10 (decimal)
Damon
  • 3,004
  • 7
  • 24
  • 28
0

The trick is that it's not parseInt which interprets your number as octal, but the JS interpreter.

This section of ECMAScript describes how numbers in source code are interpreted by the JS interpreter. This one describes the grammar for octal numbers.

Some overzelousness of eliminating the ambiguity of octal literals results in interesting scenarios :

(Chrome 32)

01.0 // SyntaxError: Unexpected number
01.9 // SyntaxError: Unexpected number
09.9 // 9.9

(function(){"use strict"; 01;})() // SyntaxError: Octal literals are not allowed in strict mode.
(function(){"use strict"; 01.0;})() // SyntaxError: Unexpected number
(function(){"use strict"; console.log(09.9);})() // 9.9

Firefox 26

01.0 // SyntaxError: missing ; before statement
09.0 // SyntaxError: missing ; before statement
09.9 // 9.9

(function(){"use strict"; 01;})() // SyntaxError: octal literals and octal escape sequences are deprecated
(function(){"use strict"; 01.0;})() // SyntaxError: octal literals and octal escape sequences are deprecated
(function(){"use strict"; 09.9;})() // SyntaxError: octal literals and octal escape sequences are deprecated
Tibos
  • 27,507
  • 4
  • 50
  • 64
0

In C type languages integer can be represented in multiple ways:

  • As ocatl number by putting 0 in front 0101126
  • As decimal by putting nothing in fron 101126
  • As hexadecimal number by putting 0x in front 0x101126

Each of these parseInt will convert to decimal base, so your octal base number is converted to decimal.

Jānis Gruzis
  • 905
  • 1
  • 10
  • 25
0

You have to put the number into "number"! The parseInt() function parses a string and returns an integer. Like this:

var example = parseInt("0000000101126");
console.log(example);
CMS
  • 704
  • 3
  • 9
  • 32