admin管理员组文章数量:1406926
I have a starting number to work from which is 0000 and increment it by one, i have that done, but the result is 1,2,3 instead of 0001,0002,0003 etc. How can I achieve this?
Thank you
I have a starting number to work from which is 0000 and increment it by one, i have that done, but the result is 1,2,3 instead of 0001,0002,0003 etc. How can I achieve this?
Thank you
Share Improve this question asked Mar 22, 2012 at 11:15 gkiddgkidd 1994 silver badges18 bronze badges2 Answers
Reset to default 6Let n
be the number. Then use the String.slice
method as follows:
var output = [], n, padded;
for (n=0; n<=9999; n++) {
padded = ('000'+n).slice(-4); // Prefix three zeros, and get the last 4 chars
output.push(padded);
}
console.log(output); // ["0000", "0001", ..., "9999"]
Demo: http://jsfiddle/qJSBg/
for (let i = 0; i <= 9; i++) {
for (let j = 0; j <= 9; j++) {
for (let k = 0; k <= 9; k++) {
for (let l = 0; l <= 9; l++) {
let yournumber = i.toString() + j.toString() + k.toString() + l.toString();
console.log(yournumber);
}
}
}
}
本文标签: javascriptIncrementing numbers starting from 0000Stack Overflow
版权声明:本文标题:javascript - Incrementing numbers starting from 0000 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744978718a2635677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论