Nodejs Number Format format(n, x, s, c)

Here you can find the source of format(n, x, s, c)

Method Source Code

Number.prototype.format = function(n, x, s, c) {
    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')'
        , num = this.toFixed(Math.max(0, ~~n));

    return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
};

Related

  1. format(n, x)
    Number.prototype.format = function(n, x) {
        var re = '(\\d)(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
        return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$1,');
    };
    
  2. format(n, x)
    Number.prototype.format = function (n, x) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
        return this.toFixed(Math.max(0, ~~n));
    };
    
  3. format(n, x, s, c)
    Number.isInteger = Number.isInteger || function(value) {
      return typeof value === 'number' && 
        isFinite(value) && 
        Math.floor(value) === value;
    };
    Number.prototype.format = function(n, x, s, c) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
            num = this.toFixed(Math.max(0, ~~n));
        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    ...
    
  4. format(n, x, s, c)
    Number.prototype.format = function(n, x, s, c) {
      var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
        num = this.toFixed(Math.max(0, ~~n));
      return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    };
    
  5. format(n, x, s, c)
    Number.prototype.format = function(n, x, s, c) {
      var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
          num = this.toFixed(Math.max(0, ~~n));
      return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    };
    
  6. format(n, x, s, c)
    Number.prototype.format = function(n, x, s, c) {
        if (arguments.length === 0) { n = 2; x = 3; s = ','; c = '.'; }
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
            num = this.toFixed(Math.max(0, Math.floor(n)));
        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    };
    
  7. format(n, x, unit)
    Number.prototype.format = function(n, x, unit) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\,' : '$') + ')';
        var concatunit = "";
        if (unit != null){ concatunit = " "+unit; }
        return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&.')+concatunit;
    
  8. formatNum(n, x, s, c)
    Number.prototype.formatNum = function(n, x, s, c) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
            num = this.toFixed(Math.max(0, ~~n));
        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    };
    
  9. formatNumber()
    Number.prototype.formatNumber = function() {
      var a = Math.abs(this).toString().split('');
      v = '';
      var n = 0;
      while (a.length > 0) {
        if (n>0 && n%3 == 0)
          v = '.' + v;
        v = a.pop() + v;
        n ++;
    ...