stockton Posted October 11, 2007 Share Posted October 11, 2007 I have a string = "305208001" and I need to use it in my database with a zero in front. In other words the database field must be 10 digits long and in this case prefixed with a zero. I have tried if (lastnum.length < 10) lastnum = 0 + lastnum; but when I do an alert it seems to supress the zero. alert("BatchSize = "+bsize+" FirstNumber = "+firstnum+" Lastnum = "+lastnum); I have also tried if (lastnum.length < 10) lastnum = 0 + lastnum.toString(); as well as lastnum.toString(); if (lastnum.length < 10) lastnum = 0 + lastnum; all without success. Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted October 11, 2007 Share Posted October 11, 2007 hi im no js expert, but i think using 0 + x makes it a numeric function, and using '0' + x makes it a string frunction, so try something like if (lastnum.length < 10) lastnum = '0' + lastnum; or if (lastnum.length < 10) lastnum = '0' + lastnum.toString(); but you probably want to write an actual function for this as the number might only be 8 characters long etc something like function strpad(num) { while (num.length < 10) { num = '0' + num.toString(); } return num; } lastnum = strpad(lastnum); good luck, tdw Quote Link to comment Share on other sites More sharing options...
leonglass Posted October 11, 2007 Share Posted October 11, 2007 I'm not much of an expert here either but I think you can set up the field in your database to pad with zeros. All you would have to do is pass it in. Quote Link to comment Share on other sites More sharing options...
stockton Posted October 12, 2007 Author Share Posted October 12, 2007 Sorry to say but the suggested '0' + num.toString(); did not work. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 12, 2007 Share Posted October 12, 2007 try String('0') + String(num). Quote Link to comment Share on other sites More sharing options...
stockton Posted October 13, 2007 Author Share Posted October 13, 2007 Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.