Nodejs Number Format comma()

Here you can find the source of comma()

Method Source Code

// Add a comma every 3 digits, e.g. turn 11386040 into 11,386,040
var d3 = /(\d+)(\d{3})/;
Number.prototype.comma = function() {
    var s = this.toString();
    while (d3.test(s)) s = s.replace(d3, '$1,$2');
    return s;/*from   w w  w  . j a  v  a  2s  . c  o  m*/
};

Related

  1. withCommas()
    Number.prototype.withCommas = function() {
        return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    };
    
  2. withCommas()
    Number.prototype.withCommas = function() {
      var x = 3;
      var y = parseFloat(this).toString().reverse();
      while (x < y.length) {
        y = y.substring(0, x) + "," + y.substring(x); 1
        x += 4;
      return y.reverse();
    };
    ...
    
  3. withCommas()
    Number.prototype.withCommas = function() {
      var num = this;
      return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    
  4. addTrailingZero()
    Number.prototype.addTrailingZero = function(){
      if(this < 10)  return "0"+this;
      else  return this;
    };
    
  5. addZero()
    Number.prototype.addZero=function(){
        var val=(this+"");
        val=((2>val.length)?("0"+val):val);
        return val;
    
  6. commafy()
    Number.prototype.commafy = function () {
      return String(this).commafy();
    
  7. commafy()
    Number.prototype.commafy = function () {
      return String(this).commafy();
    String.prototype.commafy = function () {
      return this.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
        return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
      });
    
  8. format()
    Number.prototype.format = function () {
      var str = this.toString();
      var Re = /[^0-9]/g;
      var ReN = /(-?[0-9]+)([0-9]{3})/;
      str = str.replace(Re,'');              
      while (ReN.test(str)) { 
        str = str.replace(ReN, "$1,$2"); 
      return str;
    ...
    
  9. format()
    Number.prototype.format = function() {
      var n = this, decPlaces = 2, decSeparator = ".", thouSeparator = ",", sign = n < 0 ? "-" : "", i = parseInt( n = Math.abs(+n || 0).toFixed(decPlaces)) + "", 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) : "");
    };
    function isNumber(n) {
      return !isNaN(parseFloat(n)) && isFinite(n);
    var dayNames = new Array("Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata");
    var monthNames = new Array("Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octobmrie", "Noiembrie", "Decembrie");
    ...