Nodejs Number Format Pad leftZeroFill(number, targetLength)

Here you can find the source of leftZeroFill(number, targetLength)

Method Source Code

/**/*  w w  w.  j a  v  a2  s .com*/
 *
 * @param number
 * @param targetLength
 * @returns {string}
 */
function leftZeroFill(number, targetLength) {
    var output = number + '';
    while (output.length < targetLength) {
        output = '0' + output;
    }
    return output;
}

Related

  1. padWithDigits(n)
    Number.prototype.padWithDigits = function (n) {  
      var txt = this.toString();
      while(txt.length < n) {
        txt = '0' + txt;
      return txt;  
    };
    
  2. padZeros(n)
    Number.prototype.padZeros = function(n){
      return this.toString().padZeros(n);
    };
    
  3. pad(length)
    Number.prototype.pad = function pad(length) {
      var str = '' + this;
      while (str.length < length) {
        str = '0' + str;
      return str;
    };
    
  4. LenWithZero(oCount)
    Number.prototype.LenWithZero = function(oCount) {
        var strText = this.toString();
        while (strText.length < oCount) {
            strText = '0' + strText;
        return strText;
    };
    
  5. leadingZero(length)
    Number.prototype.leadingZero = function(length) {
        return ('0'+this).slice(-length);
    
  6. 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;
    };
    ...
    
  7. 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;
    };
    ...
    
  8. 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;
    };
    ...
    
  9. zf(len)
    Number.prototype.zf = function(len){
        if(isNaN(len)) len = 2;
        return (this + '').pad(len, '0');
    };