【发布时间】:2025-11-21 18:05:01
【问题描述】:
可能重复:
JavaScript equivalent to printf/string.format
How can I create a Zerofilled value using JavaScript?
我在变量中有一个数字:
var number = 5;
我需要将该数字输出为 05:
alert(number); // I want the alert to display 05, rather than 5.
我该怎么做?
我可以手动检查数字并将 0 作为字符串添加到其中,但我希望有一个 JS 函数可以做到这一点?
【问题讨论】:
-
函数 zeroFill(n,l){return ('0000000000'+n).slice(-l);}
标签: javascript math numbers digits