Pad a number with leading 0 - Node.js Number

Node.js examples for Number:Format

Description

Pad a number with leading 0

Demo Code

Number.prototype.pad = function(size) {
  var s = String(this);
  if (typeof(size) !== "number") {
    size = 2;/*w ww  .j  a v  a2  s.co m*/
  }

  while (s.length < size) {
    s = "0" + s;
  }
  return s;
}

Related Tutorials