remove leading zeros

Discussion in 'JavaScript' started by mero2020, Feb 12, 2014.

  1. #1
    Hi

    is there away to remove leading zeros from string like this

    008,002,004
    to bo like this

    8,2,4

    any help??
     
    mero2020, Feb 12, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
  3. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    sorry, but this not working

    s=parseInt(002,003);
    output

    2

    i want output
    2,3
     
    Last edited: Feb 12, 2014
    mero2020, Feb 12, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You can't just blindly pass them as a group, parseInt must be run on each value individually.

    first=parseInt(002)
    second = parseInt(003);
     
    deathshadow, Feb 12, 2014 IP
  5. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #5
    ok, any other function can remove all??
     
    mero2020, Feb 12, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    How do you have them stored? In an array?

    // assumes values is an array of numbers with leading zeros
    for (var t=0; t < values.length; t++) values[t] = parseInt(values[t]);

    What are you doing with them? How are you passing them? How are they stored? You're really not giving us a lot to work with information-wise.

    Wait, is this in a string? You'd have to split the string by comma's (assuming it's a common delimiter)...
     
    deathshadow, Feb 12, 2014 IP
  7. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #7
    It's file name and I want to used it without zeros
    for example file name(003,004)
    to be like this ( bno 3,4)
     
    mero2020, Feb 12, 2014 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    This should work:

    
    var string = '008,002,0040';
    string = string.split(',').map(function(el){ return el.replace(/^0+/g,"");}).join(',');
    alert(string);
    
    Code (markup):
    http://jsfiddle.net/xQ7vE/
     
    ThePHPMaster, Feb 12, 2014 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Oddly, on filenames I'd keep the leading zeros, that way you can actually sort them properly since most OS will sort (for example) file11.png before file2.png

    I've even written code in the past to FORCE filenames to leading zeros... seems strange to want to go the other direction.
     
    deathshadow, Feb 12, 2014 IP
  10. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #10
    Thanks ThePHPMaster for your reply, it works well.
     
    mero2020, Feb 13, 2014 IP