Nodejs Number Format Pad zf(len)

Here you can find the source of zf(len)

Method Source Code

/**/*from  w w  w  . java  2 s .  c  o m*/
 * @author Dustin Moorman <dustin.moorman@gmail.com>
 * A little type augmentation for methods I use in the
 * chronograph.
 */

/**
 * Give Number a zero fill method.
 * @param len
 * @return String
 */
Number.prototype.zf = function(len){

    if(isNaN(len)) len = 2;

    return (this + '').pad(len, '0');
};

Related

  1. leadingZero(length)
    Number.prototype.leadingZero = function(length) {
        return ('0'+this).slice(-length);
    
  2. leftZeroFill(number, targetLength)
    function leftZeroFill(number, targetLength) {
        var output = number + '';
        while (output.length < targetLength) {
            output = '0' + output;
        return output;
    
  3. leftZeroPad(numZeros)
    Number.prototype.leftZeroPad = function(numZeros) {
            var n = Math.abs(this);
            var zeros = Math.max(0, numZeros - Math.floor(n).toString().length );
            var zeroString = Math.pow(10,zeros).toString().substr(1);
            if( this < 0 ) {
                    zeroString = '-' + zeroString;
            return zeroString+n;
    };
    ...
    
  4. zeroPad(len)
    Number.prototype.zeroPad = function (len) {
      var s = String(this);
      var c = '0';
      len = len || 2;
      while (s.length < len) {
        s = c + s;
      return s;
    };
    ...
    
  5. zeropad(n)
    Number.prototype.zeropad = function(n) {
       var str = "" + this;
       var len = n - str.length;
       var pad = "";
       while (len-- > 0) {
          pad += "0";
       return pad + str;
    };
    ...
    
  6. zfill(length)
    Number.prototype.zfill = function(length) { 
        var str = this.toString();
        if (str.length < length) {
            for(var i = 0; i < length - str.length; i++) str = '0' + str
        return str;
    };
    
  7. zfill(n)
    Number.prototype.zfill = function (n) {
        var str = String(this);
        while (str.length < n) {
            str = "0" + str;
        return str;
    };