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. Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/ 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 Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/#findComment-367076 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. Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/#findComment-367199 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. Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/#findComment-367653 Share on other sites More sharing options...
fenway Posted October 12, 2007 Share Posted October 12, 2007 try String('0') + String(num). Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/#findComment-367839 Share on other sites More sharing options...
stockton Posted October 13, 2007 Author Share Posted October 13, 2007 Thank you. Link to comment https://forums.phpfreaks.com/topic/72777-solved-concatinate-a-zero-at-front-of-string/#findComment-368474 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.