Nodejs Number Format Pad pad(size, decimalSize, decimalChar)

Here you can find the source of pad(size, decimalSize, decimalChar)

Method Source Code

Number.prototype.pad = function (size, decimalSize, decimalChar) {
    if (!decimalChar) decimalChar = '.';

    var str = this.toString();
    str = str.split(".");

    var result = str[0].pad("0", size ? size : 0);
    if (str.length==2){
        result += decimalChar + str[1].pad("0", decimalSize, true);
    }/*ww w  .  j a v  a 2 s  .co m*/

    return result;
}

/**
 * Generates a random integer
 * @param max
 * @param min
 * @returns {number}
 */
Math.randomInt = function(max, min){
    if(!min) min = 0;
    return Math.floor(Math.random()*(max-min))+min;
}

/**
 * Pads a string (e.g.: "9" may become "009" and "10" "010").
 * @param character
 * @param size
 * @param [right]
 * @returns {String}
 */
String.prototype.pad = function (character, size, right) {
    var s = this+"";
    if (!right){
        while (s.length < size) s = character + s;
    }else{
        while (s.length < size) s = s + character;
    }
    return s;
}

Related

  1. pad(size)
    Number.prototype.pad = function(size) {
        var s = String(this);
        while (s.length < (size || 2)) {s = "0" + s;}
        return s;
    
  2. pad(size)
    import moment from "moment";
    const TARGET = moment('2015-11-28 21:30:00+0100').utc();
    let hh = document.getElementById('H');
    let mm = document.getElementById('M');
    let ss = document.getElementById('s');
    let cc = document.getElementById('cs');
    let countdown = document.getElementById('countdown');
    Number.prototype.pad = function(size) {
      var s = String(this);
    ...
    
  3. pad(size)
    Number.prototype.pad = function(size) {
      var s = String(this);
      while (s.length < (size || 2)) {
        s = "0" + s;
      return s;
    
  4. pad(size)
    'use strict';
    Number.prototype.pad = function(size) {
      if(typeof(size) !== "number"){size = 2;}
      var s = String(this);
      while (s.length < size) s = "0" + s;
      return s;
    
  5. pad(size)
    Number.prototype.pad = function(size) {
      var s = String(this);
      if (typeof(size) !== "number") {
        size = 2;
      while (s.length < size) {
        s = "0" + s;
      return s;
    ...
    
  6. padLeft(base, chr)
    Number.prototype.padLeft = function (base, chr) {
        var len = (String(base || 10).length - String(this).length) + 1;
        return len > 0 ? new Array(len).join(chr || '0') + this : this;
    function formatDate(d) {
        return [(d.getFullYear()).padLeft(),
                (d.getMonth() + 1).padLeft(),
                d.getDate().padLeft()].join('-') +
            ' ' +
    ...
    
  7. padLeft(base, chr)
    Number.prototype.padLeft = function (base, chr) {
        var len = (String(base || 10).length - String(this).length) + 1;
        return len > 0 ? new Array (len).join(chr || '0') + this : this;
    };
    Array.prototype.compare = function (array) {
        if (!array)
            return false;
        if (this.length != array.length)
            return false;
    ...
    
  8. padLeft(base, chr)
    Number.prototype.padLeft = function (base, chr) {
        var len = (String(base || 10).length - String(this).length) + 1;
        return len > 0 ? new Array(len).join(chr || '0') + this : this;
    };
    
  9. padLeft(base,chr)
    Number.prototype.padLeft = function(base,chr) {
      var len = (String(base || 10).length - String(this).length)+1;
      return len > 0? new Array(len).join(chr || '0')+this : this;
    };