0 leading Zeros

- Posted in Dev by - Permalink

Say you want to get a number with leading zeros;

e = e.toString();

// This only works with a maximum length of 2
e = (e.length < 2) ? ("0" + e) : e;
// 9 -> "09"

e = leadingZeros(e, theLengthYouWant);

// This one is more for the much larger numbers
function leadingZeros(num, toLength) {
  while(num.length < toLength) {
    num = "0" + num;
  }
  return num;
}