0

Possible Duplicate:
Javascript parseInt gives very unexpected results

Can someone explain why javascript interprets parseInt(08) and parseInt(09) as zero but does everything right on 01 to 07?

parseFloat will give me the correct values for those values, I just couldn't find an answer, why javascript will only do this to the 8s and 9s.

For your information: the leading zeros are from times (hours:minutes).

Please have a look at the following link for a better example: http://jsfiddle.net/NPtzA/3/ There you can see this unexpected behaviour.

Community
  • 1
  • 1
Tim
  • 5,893
  • 3
  • 35
  • 64
  • Just as a side note: If your whole string is a number (i.e. no suffix), you can just use unary `+` to convert it to an (decimal) integer or float value: `var number = +numberString;` (which is also faster). – Felix Kling May 09 '11 at 09:43
  • Try `parseInt(08,10)` and `parseInt(09,10)`. – robertc May 09 '11 at 09:43
  • Yes! You're right, thanks for finding this, now I can see it in the related questions, too! – Tim May 09 '11 at 09:44
  • 1
    Side note 2: http://www.jslint.com/ gives you an error if you don't pass the radix parameter. – Felix Kling May 09 '11 at 09:46

1 Answers1

0

parseInt returns 0 because the function is trying to determine the right base the numerical system it's looking at.

Whether it's a good thing or not, JavaScript numbers with a leading 0 are all considered as Octal which means there is no 08 or 09.

Hope that helps.

Edit: Check out my edit of your fiddle which adds 10 parameter to parseInt to change the numerical system it uses http://jsfiddle.net/jbrooksuk/NPtzA/5/

James
  • 5,137
  • 5
  • 40
  • 80