NOTE : Potentially outdated.
(注意 :可能已过时。)
You're asking for zero padding?
(您是否要求零填充?)
Not really rounding.(不是真正的舍入。)
You'll have to convert it to a string since numbers don't make sense with leading zeros.(您必须将其转换为字符串,因为数字对于前导零没有意义。)
Something like this...(像这样)
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
Or if you know you'd never be using more than X number of zeros this might be better.
(或者,如果您知道您永远不会使用超过X个零的数字,那可能会更好。)
This assumes you'd never want more than 10 digits.(这假设您永远不要超过10位数字。)
function pad(num, size) {
var s = "000000000" + num;
return s.substr(s.length-size);
}
If you care about negative numbers you'll have to strip the "-" and readd it.
(如果您关心负数,则必须去除“-”并读取它。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…