I have a number in Javascript, that I know is less than 10000 and also non-negative. I want to display it as a four-digit number, with leading zeroes. Is there anything more e
I think the most compact yet intuitive way is:
function toFixedLength(input, length, padding) { padding = String(padding || "0"); return (padding.repeat(length) + input).slice(-length); }
The slice method here could be replaced with substr if that is more intuitive to the coder.
slice
substr