Nodejs Number Format Pad pad(length, char)

Here you can find the source of pad(length, char)

Method Source Code

Number.prototype.pad = function(length, char) {
   var str = '' + this;
   while (str.length < length) {
      str = char + str;//from w ww  . ja  va  2s . c  om
   }
   return str;
}

var flare = {};
flare.hosts = {};
for(var i = 1; i < 6; i++) {
   var forests = {}
   var max = Math.floor(Math.random()*11) + 1;
   for(var j = 0; j < max; j++) {
      forests["forest" + j.pad(3, "0")] = Math.floor(Math.random()*800) + 1;
   }
   flare.hosts["host" + i.pad(3, "0")] = forests;
}

console.dir(flare);

Related

  1. pad(len)
    Number.prototype.pad = function(len) {
      s = num.toString();
      if (s.length < len) {
        s = ('00000000000000000000' + s).slice(-len);
      return s;
    };
    
  2. pad(len)
    Number.prototype.pad = function (len) {
        return (new Array(len+1).join("0") + this).slice(-len);
    function updateClock() {
      var time = new Date()
      if (time.getHours() > 12)
        elements.clock.html(time.getHours() - 12 + ":" + time.getMinutes() + " PM")
      elements.clock.html(time.getHours() + ":" + time.getMinutes().pad(2) + " AM");
    Number.prototype.toRad = function() {
        return this * Math.PI / 180;
    
  3. pad(length)
    Number.prototype.pad = function(length) {
        var result = this.toString(),
            pad = length - result.length;
        while(pad > 0) {
            result = '0' + result;
            --pad;
        return result;
    };
    ...
    
  4. pad(length)
    Number.prototype.pad = function(length) {
      var _num = this.toString();
      if (_num.length > length) { 
        return _num;
      return (new Array(length).join('0') + this).slice(-length);
    };
    
  5. pad(length, char)
    Number.prototype.pad = function(length, char) {
        if (length === undefined)
            length = 2;
        if (char === undefined)
            char = "0";
        return (char + this).slice(length * -1);
    
  6. pad(n)
    Number.prototype.pad = function(n) {
      var value = this.toString();
      var length = n + 1 - value.length;
      return length > 0 ? (new Array(length)).join("0") + value: value;
    };
    
  7. pad(n)
    Number.prototype.pad = function(n) {
        return ('0000000000' + this).slice((n || 2) * -1);
    
  8. pad(n, p)
    Number.prototype.pad = function(n, p) {
      var s = '' + this;
      p = p || '0';
      while (s.length < n) s = p + s;
      return s;
    
  9. pad(size)
    Number.prototype.pad = function(size) {
      var s = String(this);
      while (s.length < (size || 2)) { s = '0' + s; }
      return s;
    };