Nodejs Number Format format(n, x)

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

Method Source Code

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

Related

  1. format(digit, separator)
    Number.prototype.format = function(digit, separator)
        if (typeof digit != 'number' || digit > 99) 
            throw new Error('Number.toFormattedString - Wrong argument.');
        var decimals = Math.round(((this - Math.floor(this)) * Math.pow(10, digit))).toString();
        while (digit - decimals.length > 0) 
            decimals = '0' + decimals;
        if (typeof separator != 'string' || separator.length != 1) 
            separator = '.';
        return Math.floor(this).toString() + separator + decimals.substr(0, digit);
    };
    
  2. format(fmt)
    Number.prototype.format = function (fmt) {
        var s = this.toFixed(2).toString();
        var decimals = "";
        if (s.indexOf(".") > 0) {
            var arr = s.split('.');
            s = arr[0]; decimals = arr[1];
        if (this > 0 && this < 1000) {
            if (decimals != "" && parseInt(decimals) != 0) {
    ...
    
  3. format(k, fixLength)
    Number.decPoint = ',';
    Number.thousand_sep = '.';
    Number.prototype.format = function (k, fixLength) {
        if (!k) k = 0;
        var neu = '';
        var sign = this < 0 ? '-' : '';
        var f = Math.pow(10, k);
        var zahl = Math.abs(this);
        zahl = '' + parseInt(zahl * f + .5) / f;
    ...
    
  4. 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'), '$&,');
    };
    
  5. 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,');
    };
    
  6. 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 || ','));
    ...
    
  7. 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 || ','));
    };
    
  8. 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 || ','));
    };
    
  9. 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 || ','));
    };