-3

Possible Duplicate:
How to parseInt a string with leading 0

If I parseInt("01") in javascript its not the same as parseInt("1")???

start = getStartEugene("MN01");
start2 = getStartEugene("MN1");

getStartEugene: function(spot)  //ex: GT01 GT1
{
    var yard = spot.match(/[0-9]+/);
    var yardCheck = parseInt(yard);
    if (yardCheck < 10)
       return "this"+yard;
    else
      return "this0"+yard
}

I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09

But i am not getting it. Anyone know why?

Community
  • 1
  • 1
ealeon
  • 12,074
  • 24
  • 92
  • 173

2 Answers2

3

You need to add the radix (2nd) argument to specify you are using a base 10 number system...

parseInt("01", 10); // 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • this isnt the solution. maybe i need to implement this in other ways besides parsing the int to classify blah01 and blah1 as the same – ealeon Oct 03 '12 at 16:29
  • @ealeon: Why isn't it the solution? It is... it will correctly parse numeric strings with leading zeros. – Felix Kling Oct 03 '12 at 16:47
1

This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as @jondavidjohn correctly pointed).

parseInt("10");  // returns 10
parseInt("010"); // returns 8
Gerardo Lima
  • 6,467
  • 3
  • 31
  • 47