I am trying to do something like this;
when i = 1 string ="0001"
when i = 10 string = "0010"
etc
When I did a serach I found Xbytor's _zeroPad functon but it only adds 1 zero if less than 10. I can change that with if statements but I thougt I would ask first if there is a better way
Thanks,
Mike
Padding Zeros to an Index eg 10 becomes 0010
Padding Zeros to an Index eg 10 becomes 0010
Heh heh, how many mad ways can we find to do this. How about this one:
var i = 10;
var z = 10000;
var s =( i + z).toString().substr(1);
Andrew
var i = 10;
var z = 10000;
var s =( i + z).toString().substr(1);
Andrew
Padding Zeros to an Index eg 10 becomes 0010
var i = '10';
var z = '0000';
var s = z.substr(0,z.length - i.length) + i;
This is fun.
Andrew
var z = '0000';
var s = z.substr(0,z.length - i.length) + i;
This is fun.
Andrew
Padding Zeros to an Index eg 10 becomes 0010
That works great.
Would you explain how it works incase I need to change the number of leading 0s.
On a grade school level
Would you explain how it works incase I need to change the number of leading 0s.
On a grade school level
Padding Zeros to an Index eg 10 becomes 0010
The first just adds the numbers as numbers 10010 the converts that to a string then takes off the first character of the string. If they were a string to start with you might have to convert them to a number first, but then you can use the second method whichis entirely string based. The second method can be used with i being a number by converting it to a string first.
alert(Number(10).toString() == '10');
Andrew
alert(Number(10).toString() == '10');
Andrew
Padding Zeros to an Index eg 10 becomes 0010
Ok, one last question. Do you see anything right off that looks wrong with this function?
Code: Select allfunction zeroPad(i,pad) {
var z = Math.pow(10,pad)
var s =( i + z).toString().substr(1);
return s;
}
alert(zeroPad(23,4))
Code: Select allfunction zeroPad(i,pad) {
var z = Math.pow(10,pad)
var s =( i + z).toString().substr(1);
return s;
}
alert(zeroPad(23,4))
Padding Zeros to an Index eg 10 becomes 0010
You might be safer to use:
var s =(Number( i) + z).toString().substr(1);
Then it will work if i is a number or a string. I'm going to move this to the code snippets forum.
Andrew
var s =(Number( i) + z).toString().substr(1);
Then it will work if i is a number or a string. I'm going to move this to the code snippets forum.
Andrew
Padding Zeros to an Index eg 10 becomes 0010
In that case I am changing z to this var z = Math.pow(10,Number(pad)) right?
Thanks for all your help with this
Mike
Thanks for all your help with this
Mike
Padding Zeros to an Index eg 10 becomes 0010
This is the string version, it is possibly marginally more efficient:
Code: Select allfunction zeropad (n,l) {
n = n.toString();
l = Number(l);
var pad = '0';
while(n.length < l) {n = pad + n;}
return n;
}
var n = 10;
var l = 4;
Andrew
Code: Select allfunction zeropad (n,l) {
n = n.toString();
l = Number(l);
var pad = '0';
while(n.length < l) {n = pad + n;}
return n;
}
var n = 10;
var l = 4;
Andrew
Padding Zeros to an Index eg 10 becomes 0010
The first several code snippets are potentially dangerous. What if the number you're starting with already has more digits than the maximum? In that case, the first digits will get cut off, e.g. 123456 will become 3456.
Andrew's final code snippet is the only one that behaves correctly in that case. (12 becomes 0012, but 123456 remains 123456).
Andrew's final code snippet is the only one that behaves correctly in that case. (12 becomes 0012, but 123456 remains 123456).