Nodejs Number Format formatformat(n, x)

Here you can find the source of formatformat(n, x)

Method Source Code

/* eslint no-extend-native: ["error", { "exceptions": ["Number", "String", "Array"] }] */

'use strict';//from   w  ww.  j a  v  a2 s  .  c  o m

/**
 * Number.prototype.format(n, x)
 *
 * @param {integer} n length of decimal
 * @param {integer} x length of sections
 * @return {string} Formatted number
 */
Number.prototype.format = function format(n, x) {
  const re = `\\d(?=(\\d{${x || 3}})+${(n > 0 ? '\\.' : '$')})`;
  return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
};

String.prototype.rpad = function rpad(padString, length) {
  let str = this;
  while (str.length < length) {
    str = str + padString;
  }
  return str;
};

const Utils = {};

Utils.shuffle = (arr) => {
  const a = arr;

  for (
    let j, x, i = a.length;
    i;
    j = Math.floor(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);

  return a;
};

module.exports = Utils;

Related

  1. formatBytes()
    Number.prototype.formatBytes = function() {
        var units = ['Bytes', 'KBytes', 'MBytes', 'GBytes'],
            bytes = this,
            i;
        for (i = 0; bytes >= 1024 && i < 3; i++) {
            bytes /= 1024;
        return bytes.toFixed(2) + ' ' + units[i];
    };
    ...
    
  2. formatComma()
    Number.prototype.formatComma = function() {
      var n = this;
      var thouSeparator = thouSeparator == undefined ? "," : thouSeparator;
      var sign = n < 0 ? "-" : "";
      var i = parseInt(n = Math.abs(+n || 0)) + "";
      var j = (j = i.length) > 3 ? j % 3 : 0;
      return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator); 
    };
    
  3. formatCustom(c, d, t)
    Number.prototype.formatCustom = function(c, d, t){
    var n = this, 
        c = isNaN(c = Math.abs(c)) ? 2 : c, 
        d = d == undefined ? "." : d, 
        t = t == undefined ? "," : t, 
        s = n < 0 ? "-" : "", 
        i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))), 
        j = (j = i.length) > 3 ? j % 3 : 0;
       return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
    ...
    
  4. formatDecimal()
    Number.prototype.formatDecimal = function() {
      var nStr = this.toFixed(2);
      x = nStr.split('.');
      x1 = x[0];
      x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
      return x1 + x2;
    };
    
  5. formatHuman()
    Number.prototype.formatHuman = function() {
      var numStr   = '' + this;
      var numParts = numStr.split('.');
      var whole    = numParts[0];
      var decimal  = numParts[1] || '';
      var re = /(\d+)(\d{3})/;
      while (re.test(whole)) {
        whole = whole.replace(re, '$1' + ',' + '$2');
      return decimal.length === 0 ? whole : whole + '.' + decimal;
    };
    
  6. formatted()
    Number.prototype.formatted = function(){
      if (!this) return 0;
      var sign   = this < 0 ? '-' : '',
        num    = Math.floor(Math.abs(this)*100+0.50000000001),
        cents  = (num % 100).toString().substring(0, 2);
      num = Math.floor(num/100).toString();
      for (var i = 0, l = Math.floor((num.length-1)/3), p = 3; i < l; i++, p += 4){
        num = num.substring(0, num.length - p) + ' ' + num.substring(num.length - p);
      return sign + num + '.' + (cents < 10 ? '0' + cents : cents);
    };
    
  7. toSize()
    Object.defineProperty( Number.prototype, "toSize", {
        "get": function() {
            return function( bitsNotBytes ) {
                var sizes = ['', ' Kb', ' Mb', ' Gb', ' Tb', ' Pb' ];
                var unit  = 0, remain = Math.abs( this * 1 );
                if ( bitsNotBytes )
                    remain *= 8;
                while ( remain >= 1024 && unit<5 )
                    remain /= 1024, unit++;
    ...
    
  8. CommaFormatted(n,symbol)
    Number.prototype.CommaFormatted = function(n,symbol) {
      return (symbol || '\uffe5') + this.toFixed(n === void 0 ? 2 : n).toString().replace(/(\d)(?=(\d{3})+($|\.))/g, '$1,');
    };
    (4564546).CommaFormatted(2,"$");
    
  9. numberFormat()
    String.prototype.numberFormat=function() {
       return this.replace(/(\d)(?=(?:\d{3})+(?!\d))/g,'$1,');
    function numberformatter(vNum){
      return String(vNum).numberFormat();