Nodejs Number Format formatComma()

Here you can find the source of formatComma()

Method Source Code

Number.prototype.formatComma = function() {
  var n = this;//from www . ja va 2  s.c o m
  // decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
  // decSeparator = decSeparator == undefined ? "." : decSeparator,
  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); //+ (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};

Related

  1. formatNumber(c, d, t)
    Number.prototype.formatNumber = 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 = parseInt(n = Math.abs(+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) : "");
    ...
    
  2. formatNumber(c, d, t)
    Number.prototype.formatNumber = 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 = parseInt(n = Math.abs(+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) : "");
    
  3. format(thousandSep, decPoint)
    Number.THOUSAND_STEP = ",";
    Number.DECIMAL_POINT = ".";
    Number.prototype.format = function(thousandSep, decPoint) {
      var decimal, str;
      thousandSep = thousandSep || Number.THOUSAND_STEP;
      decPoint = decPoint || Number.DECIMAL_POINT;
      decimal = this.toString().split(".");
      str = Math.abs(this)
        .toFixed(0)
    ...
    
  4. formatAmount(c, d, t)
    Number.prototype.formatAmount=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 = parseInt(n = Math.abs(+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) : "");
    ...
    
  5. 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];
    };
    ...
    
  6. 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) : "");
    ...
    
  7. 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;
    };
    
  8. 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;
    };
    
  9. formatformat(n, x)
    'use strict';
    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;
    ...