By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

trouble using js to remove leading zeros

n8kindt
100+
P: 221
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     input = parseInt(input_line.replace("^0+", '')); //it's an item number... drop zeros ... maybe?
  3.  
  4. }
any suggestions??
Dec 24 '08 #1
Share this Question
Share on Google+
5 Replies


n8kindt
100+
P: 221
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

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html lang="en">
  3. <head>
  4. <script type="text/javascript">
  5. trimLeading0 = function(input){
  6.     var splitString = new Array();
  7.     var string = new String(input);
  8.     var returnString = '';
  9.     var k = 1; //length counter
  10.  
  11.     for(var i=0;i<string.length;i++){
  12.         splitString[i] = string.substring(i,k); //split the string up into an array
  13.         k++;//keep the length counter going
  14.     }
  15.     for (var j=0;j<splitString.length;j++){
  16.         if(splitString[j]=='0' ||  splitString[j]==0){
  17.             //do nothing and wait for a nonzero number
  18.         }
  19.         else{
  20.             for(j;j<splitString.length;j++){
  21.                 returnString = returnString +""+ splitString[j]; //put humpty dumpty back together
  22.             }
  23.         }
  24.     }
  25.     return returnString;   
  26. }
  27. </script>
  28. </head>
  29. <body>
  30.     <script type="text/javascript">
  31.     document.write('00548<br>');
  32.     document.write(trimLeading0('00548'))
  33.     </script>
  34.  
  35. </body>
  36. </html>
  37.  
Dec 24 '08 #2

acoder
Expert Mod 15k+
P: 16,027
The regexp. would work if you used the correct syntax - either
Expand|Select|Wrap|Line Numbers
  1. input = parseInt(input_line.replace(/^0+/, ''));
or
Expand|Select|Wrap|Line Numbers
  1. input = parseInt(input_line.replace(new RegExp("^0+"), ''));
Dec 24 '08 #3

rnd me
Expert 100+
P: 427
a much faster/simpler way:

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

always use 10 when converting to a base-10 number...
Dec 24 '08 #4

Expert 100+
P: 173
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).
Dec 24 '08 #5

n8kindt
100+
P: 221
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.
Dec 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.