Add leading zeros to a number in Javascript
Needed a quick way to add leading zeros to numbers for a js countdown script. Not hard to do, now I can look at this post for the code when I need it again.
function addLeadingZeros(n, totalDigits) { if(!totalDigits)totalDigits = 2; n = String(n); while (n.length < totalDigits) n = "0"+n; return n; } var num = 5; num = addLeadingZeros(num, 3);