本文翻译自:How to output numbers with leading zeros in JavaScript [duplicate]
Possible Duplicate: 可能重复:
How can I create a Zerofilled value using JavaScript? 如何使用JavaScript创建零填充值?
I can round to x amount of decimal places with math.round but is there a way to round left of the decimal? 我可以用math.round舍入到小数位数的x位数,但是有没有办法舍入到小数点左边? for example 5 becomes 05 if I specify 2 places 例如,如果我指定2个位置,则5变为05
#1楼
参考:https://stackoom.com/question/Ca7U/如何在JavaScript中输出带有前导零的数字
#2楼
You could extend the Number object: 您可以扩展Number对象:
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
Examples: 例子:
(9).pad(); //returns "09"
(7).pad(3); //returns "007"
#3楼
NOTE : Potentially outdated. 注意 :可能已过时。 ECMAScript 2017 includes String.prototype.padStart ECMAScript 2017包含String.prototype.padStart
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. 如果您关心负数,则必须去除“-”并读取它。
#4楼
UPDATE: Small one-liner function using the ES2017 String.prototype.padStart method: 更新:使用ES2017 String.prototype.padStart方法的小型一线函数:
function zeroPad(num, places) { return String(num).padStart(places, '0') } console.log(zeroPad(5, 2)); // "05" console.log(zeroPad(5, 4)); // "0005" console.log(zeroPad(5, 6)); // "000005" console.log(zeroPad(1234, 2)); // "1234"
Another ES5 approach: ES5的另一种方法:
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
zeroPad(5, 2); // "05"
zeroPad(5, 4); // "0005"
zeroPad(5, 6); // "000005"
zeroPad(1234, 2); // "1234" :)
#5楼
Just for fun (I had some time to kill), a more sophisticated implementation which caches the zero-string: 只是为了好玩(我有时间消磨时间),这是一个更复杂的实现,它缓存零字符串:
pad.zeros = new Array(5).join('0');
function pad(num, len) {
var str = String(num),
diff = len - str.length;
if(diff <= 0) return str;
if(diff > pad.zeros.length)
pad.zeros = new Array(diff + 1).join('0');
return pad.zeros.substr(0, diff) + str;
}
If the padding count is large and the function is called often enough, it actually outperforms the other methods... 如果填充数很大并且经常调用该函数,则它实际上胜过其他方法...
#6楼
From https://gist.github.com/1180489 从https://gist.github.com/1180489
function pad(a,b){return(1e15+a+"").slice(-b)}
With comments: 有评论:
function pad(
a, // the number to convert
b // number of resulting characters
){
return (
1e15 + a + // combine with large number
"" // convert to string
).slice(-b) // cut leading "1"
}
这篇博客讨论了如何在JavaScript中为数字添加前导零,以达到格式化输出的目的。提到了数学舍入与前导零填充的区别,并提供了多种实现方式,包括扩展Number对象、使用padStart方法以及一些效率更高的实现方案。文章还包含了ES5和ES2017的不同解决方案,以及处理负数的情况。
5346

被折叠的 条评论
为什么被折叠?



