Pad a number - Node.js Number

Node.js examples for Number:Format

Description

Pad a number

Demo Code


Number.prototype.pad = function(size) {
  var s = String(this);
  if (typeof(size) !== "number") {
    size = 2;//from  w w w . ja va 2 s . com
  }

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

Related Tutorials