JavaScript format number to day with always 3 digits
Solution 1
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);
}
Solution 2
I found an elegant solution by Samuel Mullen on his blog. I simply optimized the zeroes
creation.
function lpad(value, padding) {
var zeroes = new Array(padding+1).join("0");
return (zeroes + value).slice(-padding);
}
Usage: lpad(12, 3)
results in "012"
Solution 3
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.
Solution 4
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
Solution 5
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;
};
ali
I am a web, desktop and mobile applications developers with great knowledge in web development, medium knowledge in desktop applications development and a beginner status in mobile applications development.
Updated on June 01, 2020Comments
-
ali almost 4 years
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 3 it must write 003, instead of 12 it must write 012. If it is greater than 100 output 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!
-
Jonathan M almost 12 yearsYou might check the answers here: stackoverflow.com/questions/610406/…
-
-
Phrogz almost 12 yearsI think you meant
.join("0")
and I think you meant to put that before thestr
. -
VisioN almost 12 yearsThe first won't work for numbers with 4 digits and more.
-
Admin almost 12 years@VisioN: The title says "always 3 digits"
-
Admin almost 12 years...though the negative index will fail in IE8 and lower.
-
VisioN almost 12 years@amnotiam Right, but there is also another phrase which can be considered differently: If it is greater than 100 output it without formatting. Who knows where is the right way, it's good to play safe.
-
georg almost 12 years@VisioN: fair enough, post updated.
-
andreszs about 4 yearsNice but use substring rather than substr which is deprecated.
-
Will Blair about 2 years@andreszs substring does not support negative indexing ( link ). Best to use
slice()
instead.