JavaScript 格式编号,始终为 3 位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10841773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript format number to day with always 3 digits
提问by ali
Possible Duplicate:
How can I create a Zerofilled value using JavaScript?
I have to output a day number that must always have 3 digits. Instead of 3it must write 003, instead of 12it must write 012. If it is greater than 100output it without formatting. I wonder if there's a regex that I could use or some quick in-line script, or I must create a function that should do that and return the result. Thanks!
我必须输出一个必须始终为 3 位数字的日期编号。而不是3,必须写003,而不是12必须写012。如果大于100则不格式化输出。我想知道是否有我可以使用的正则表达式或一些快速的内联脚本,或者我必须创建一个应该这样做并返回结果的函数。谢谢!
回答by georg
How about:
怎么样:
zeroFilled = ('000' + x).substr(-3)
For arbitrary width:
对于任意宽度:
zeroFilled = (new Array(width).join('0') + x).substr(-width)
As per comments, this seems more accurate:
根据评论,这似乎更准确:
lpad = function(s, width, char) {
return (s.length >= width) ? s : (new Array(width).join(char) + s).slice(-width);
}
回答by Mattias Buelens
I found an elegant solution by Samuel Mullen on his blog. I simply optimized the zeroes
creation.
我在 Samuel Mullen的博客上找到了一个优雅的解决方案。我只是优化了zeroes
创作。
function lpad(value, padding) {
var zeroes = new Array(padding+1).join("0");
return (zeroes + value).slice(-padding);
}
Usage: lpad(12, 3)
results in "012"
用法:lpad(12, 3)
结果"012"
回答by Mattias Buelens
You can do this...
你可以这样做...
("00" + day).slice(-3)
It'll prepend the zeros, and then .slice()
will always give you the last 3 values of the string.
它会在前面加上零,然后.slice()
总是给你字符串的最后 3 个值。
回答by Samuel Rossille
I would write the following function:
我会编写以下函数:
var pad = function(n, length) {
var str = "" + n;
if(str.length < length) str = new Array(length - str.length).join("0") + str;
return str;
};
回答by ThiefMaster
Here is a simple function that pads a number with zeroes to a certain width:
这是一个简单的函数,它用零填充一个数字到一定的宽度:
function zeroFill(number, width) {
width -= number.toString().length;
if(width > 0) {
return new Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number;
}
return number + ""; // always return a string
}
(from How can I pad a value with leading zeros?)
(来自如何用前导零填充值?)
Since the original answer did not explain how the function works I'll do it here.
由于原始答案没有解释该函数的工作原理,我将在这里进行。
width
initially contains the total length you want, so width - number_of_digits
is the number of padding chars necessary.new Array(len + 1).join(str)
repeats str
len
times.
The regex is used to add an additional padding zero in case of a number containing a decimal point since the point was also included in the number_of_digits
determined using number.toString().length
width
最初包含您想要的总长度,因此width - number_of_digits
需要填充字符的数量。new Array(len + 1).join(str)
重复str
len
次数。
正则表达式用于在包含小数点的数字的情况下添加额外的填充零,因为该点也包含在number_of_digits
确定的使用中number.toString().length
回答by VisioN
One possible solution:
一种可能的解决方案:
?while ((val+"").length < 3?) {
val = "0" + val;
}