从 0000 开始递增的数字

gki*_*idd 3 javascript

我有一个开始工作的数字是 0000 并将其递增 1,我已经完成了,但结果是 1,2,3 而不是 0001,0002,0003 等等。我怎样才能做到这一点?

谢谢

Rob*_*b W 6

n是数字。然后使用String.slice如下方法:

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"]
Run Code Online (Sandbox Code Playgroud)

演示:http : //jsfiddle.net/qJSBg/