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
How about something like this:
function prefixZeros(number, maxDigits)
{
var length = maxDigits - number.toString().length;
if(length <= 0)
return number;
var leadingZeros = new Array(length + 1);
return leadingZeros.join('0') + number.toString();
}
//Call it like prefixZeros(100, 5); //Alerts 00100