trouble using js to remove leading zeros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n8kindt
    New Member
    • Mar 2008
    • 221

    trouble using js to remove leading zeros

    First of all, i haven't been to this site in a while and WOW is it much improved. I love all the new stuff--it looks great!

    I am all but done building a simple Point of Sale system through javascript but my current conflict is that our barcode scanner reads "785" as "00785." I've tried everything to filter out the zeros: regexp, converting to integer and multiplying/dividing by 1, and a couple other sidewall experimental approaches but the zeros still remain. Here's my current set up. Remember, all I want to do is screen the leading zeros from the input and this comes in on line 2.

    Code:
           
        input = parseInt(input_line.replace("^0+", '')); //it's an item number... drop zeros ... maybe?
      
    }
    any suggestions??
  • n8kindt
    New Member
    • Mar 2008
    • 221

    #2
    alright well i had an idea so i tried it out and it worked. here's the code i created for anyone who might run into the same problem in the future

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <head>
    <script type="text/javascript">
    trimLeading0 = function(input){
        var splitString = new Array();
        var string = new String(input);
        var returnString = '';
        var k = 1; //length counter
        
        for(var i=0;i<string.length;i++){
            splitString[i] = string.substring(i,k); //split the string up into an array
            k++;//keep the length counter going
        }
        for (var j=0;j<splitString.length;j++){
            if(splitString[j]=='0' ||  splitString[j]==0){
                //do nothing and wait for a nonzero number
            }
            else{
                for(j;j<splitString.length;j++){
                    returnString = returnString +""+ splitString[j]; //put humpty dumpty back together
                }
            }
        }
        return returnString;   
    }
    </script>
    </head>
    <body>
        <script type="text/javascript">
        document.write('00548<br>');
        document.write(trimLeading0('00548'))
        </script>
    
    </body>
    </html>

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      The regexp. would work if you used the correct syntax - either
      Code:
      input = parseInt(input_line.replace(/^0+/, ''));
      or
      Code:
      input = parseInt(input_line.replace(new RegExp("^0+"), ''));

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        a much faster/simpler way:

        alert( parseInt( "0000420", 10 ) );

        always use 10 when converting to a base-10 number...

        Comment

        • phvfl
          Recognized Expert New Member
          • Aug 2007
          • 173

          #5
          Another alternative is to simply multiply by 1 which would convert the number to an integer. A very quick test over a 1,000,000 iterations indicates that this is quicker, although both are almost instant. (parseInt: 291ms, multiply: 139ms; times for 1,000,000 iterations).

          Comment

          • n8kindt
            New Member
            • Mar 2008
            • 221

            #6
            you know the really wierd thing is i've tried all those suggestions listed (except the second regexp example and the alert( parseInt( "0000420", 10 ) ); ) and i had absolutely no luck. i will see if i can try to get those work again b/c i'm pretty sure those are quicker/lighter than my function.

            Comment

            Working...