0

Possible Duplicate:
Workarounds for JavaScript parseInt octal bug

I am trying to parse an integer number.

a = parseInt("0005")  <- gives 5
a = parseInt("0008")  <- gives 0

Can someone explain what's happening? It doesn't make any sense to me.

Community
  • 1
  • 1
Dave Heart
  • 39
  • 4
  • Thanks Pekka. This solved the problem. Can you add your comment as an answer so I can accept it and close off the questions. – Dave Heart Jun 12 '11 at 17:30

2 Answers2

2

When parseInt has a leading 0 and a radix parameter isn't specified, it assumes you want to convert the number to octal. Instead you should always specify a radix parameter like so:

a = parseInt("0008", 10) // => 8
mVChr
  • 49,587
  • 11
  • 107
  • 104
1

Numbers starting with 0 are parsed as octal by parseInt, unless you specify a radix to use.

You can force parseInt to parse as decimal by doing

a = parseInt("0008", 10)
Dogbert
  • 212,659
  • 41
  • 396
  • 397