4

A leading zero to some number converting the number to some unknown number format. for example :

017 is getting converted to 15

037 is getting converted to 31

Also found that numbers having 8 0r 9 at end are remaining same for example :

018 is 18

038 is 38

o59 is 59

one more thing that I found is

for each next range of 10 the difference between converted value and the actual value get incremented by 2

for example :

for range 00-09 difference is 0 i.e value of 07 will be 7, 04 will be 4

for range 010-019 difference is 2 value of 017 will be 15, 013 will be 11

for range 020-029 difference is 4 value of 027 will be 23, 021 will be 17

and so on..

here is a snipet for test http://jsfiddle.net/rajubera/BxQHF/

I am not getting why this is happening ?

Please help me how to get the correct decimal number from the number having leading zero ?

Raju Bera
  • 948
  • 8
  • 14
  • 4
    You're going to need to post some code so we can reproduce the issue. – Jason P Jul 31 '13 at 14:29
  • Possible duplicate of [Leading zero in javascript](http://stackoverflow.com/questions/6505033/leading-zero-in-javascript) and [Javascript, why treated as octal](http://stackoverflow.com/questions/9071696/javascript-why-treated-as-octal) – apsillers Jul 31 '13 at 14:44
  • possible duplicate of [Javascript parseInt() with leading zeros](http://stackoverflow.com/questions/8763396/javascript-parseint-with-leading-zeros) – Bergi Jul 31 '13 at 14:44
  • no in my case parseInt(012,10) doesn't help. you can check the jsfiddle link – Raju Bera Jul 31 '13 at 14:56
  • 2
    *Why* are you writing your numbers with leading zeros? If you want decimal values, just don't do that. If you are getting the values as string input, `parseInt(string,10)` will work (but you are correct that using `parseInt` on an octal literal will not convert it to decimal. – apsillers Jul 31 '13 at 15:05

2 Answers2

7

If there is a leading 0, it is converting it to octal (base 8) as long as its a valid number in base 8 (no numbers greater than 7).

For example:

017 in base 8 is 1 * 8 + 7 = 15 037 in base 8 is 3 * 8 + 7 = 31

018 is converted to 18 because 018 isn't a valid number in base 8

Note that the behavior as to which base the number is converted to by default can be browser-specific, so its important to always specify the base/radix when using parseInt:

parseInt("017",10) === 17

UPDATE based on comments:

parseInt expects a string as the first argument, so

parseInt("012",10) === 12

go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • Fixed: base 8 if its a valid base 8 number, otherwise base 10. Thanks! – go-oleg Jul 31 '13 at 14:37
  • if it was converting to octal base then 018 should return 16 but 018 is resulting to 18. – Raju Bera Jul 31 '13 at 14:38
  • I also think so..but then how can I get the correct number ?? – Raju Bera Jul 31 '13 at 14:51
  • Use parseInt(yourNumber,10) to get the correct value if your numbers are in base 10. – go-oleg Jul 31 '13 at 14:53
  • thanks but no in my case parseInt(012,10) doesn't help. please check the jsfiddle link http://jsfiddle.net/rajubera/BxQHF/ – Raju Bera Jul 31 '13 at 15:00
  • yes but is there any way that I can convert the number to string before passing to parseInt function. as when I am doing either of ''+012 or 012.toString() it's returning me "10" that means first the number is getting converted then getting stringified. – Raju Bera Jul 31 '13 at 15:10
  • As @apsillers pointed out in his comment, why are these numbers with leading zeroes to begin with? If they are coming from user input they should probably be starting out as strings, and if they aren't then they just shouldn't have leading zeroes. – go-oleg Jul 31 '13 at 15:45
1

One of the reasons to "use strict";

(function() {"use strict"; 017})()

// Firefox => SyntaxError: "0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the \"0o\" prefix instead 
// Chrome, Node => SyntaxError: Octal literals are not allowed in strict mode.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal

Aprillion
  • 21,510
  • 5
  • 55
  • 89