-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?

3
  • You don't even need parseInt here in your code. Commented Oct 3, 2012 at 16:01
  • you dont understand the question. I need to know whether its single digit or not because the input could be MT01 or MT1 @FelixKling Commented Oct 3, 2012 at 16:25
  • Ah right, I missed the "MN1" example... Commented Oct 3, 2012 at 16:46

2 Answers 2

3

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

parseInt("01", 10); // 1
Sign up to request clarification or add additional context in comments.

2 Comments

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: Why isn't it the solution? It is... it will correctly parse numeric strings with leading zeros.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.