0

I am getting passed numbers like 01,02,03,04 and up. And I would like to remove the prefixed 0 from them if it exists. But I need it to leave numbers like 10, 20, 30 as they are.

My latest attempt of which I think is overkill as is.. is:

newValue = 01;
tester(newValue);
newValue = 10;
tester(newValue);
function tester(newValue)
{
    finalValue = newValue;
    var arr = newValue.split('0');
    if(arr.length > 1)
    {
        if(arr[0] == 0)
        {
            finalValue = '';
            $.each(arr, function(v)
                   {
                        finalValue +=v;
                   });
        }
    }
//return finalValue;
    console.log(finalValue);
}

Im not sold on this method, I've just tried a number of ways and its all be no go.

chris
  • 31,824
  • 49
  • 132
  • 238
  • pls refer here: http://stackoverflow.com/questions/646628/javascript-startswith hope it helps.. – Black.Jack Nov 22 '13 at 20:15
  • If you're dealing with numbers, not strings (which your code example seems to show), then there is no difference between `01` and `1`. They are the same number. Try `alert(01)` and see what you get. – jfriend00 Nov 22 '13 at 20:24
  • @jfriend00 watch it, 08 and 09 are illegal octal numbers – mplungjan Nov 22 '13 at 20:31
  • @mplungjan - the point here is that the OP appears to be using numbers, not strings and isn't using `parseInt()` so the question is nonsense as posted. Besides `alert(09)` still shows `9`. As is, the question should be closed. – jfriend00 Nov 22 '13 at 21:13
  • @jfriend00 we don't close questions for being confused. That is not a valid reason for closing. – Robin Green Nov 23 '13 at 09:13
  • @RobinGreen - the question makes no sense. There is no leading zero on a numeric value (like in the OP's code)! Thus, there can be no answer that removes it. Look at the OP's line of code to illustrate `newValue = 01;`. That's a numeric value. – jfriend00 Nov 23 '13 at 09:13
  • @jfriend00 that is an excessively pedantic definition of "number", which is incorrect in many business contexts. I am talking about business domain concepts, not programming concepts. – Robin Green Nov 23 '13 at 09:15
  • @RobinGreen - Look at the OP's line of code: `newValue = 01;`. That's a numeric value, plain and simple. It has no leading zero once the JS parser has turned it into live JS. Thus, there is no leading zero to remove from `newValue`. There's nothing pedantic about it. That's the OP's code. And, the OP has had 13 hours of chances to change the question into one about removing leading zeroes from a string, but has not done so. – jfriend00 Nov 23 '13 at 09:15
  • If you are suggesting that the question is actually asking about a non-problem, then as I said before, that is simply confusion on the part of the asker, which is not a valid reason to close in and of itself. Many legitimate SO questions arise from a confused understanding of something or other! – Robin Green Nov 23 '13 at 09:17
  • @RobinGreen - the reason I selected for voting to close is "Unclear what you're asking" which is a valid, listed reason for closing a question and in 13 hours the OP has not made it clear. It is not clear whether they are confused about leading zeroes on numbers (what their code shows) or actually meant to ask about leading zeroes on strings (where a leading zero would actually occur). If you can show me how it is "clear" what the OP is actually asking, I'd like you to do so. – jfriend00 Nov 23 '13 at 09:22

2 Answers2

2

Any number loses all leading 0 when it is used in any calculation

Live Demo

function num(n) { return +n }

Faster and shorter than parseInt(str,10)

Note that 08 and 09 are considered illegal octal numbers if the leading 0 is not removed.

animuson
  • 50,765
  • 27
  • 132
  • 142
mplungjan
  • 134,906
  • 25
  • 152
  • 209
2

parseInt(val, 10) will remove leading zeros.

levi
  • 18,814
  • 17
  • 55
  • 68