Nodejs Currency Format setCurrency()

Here you can find the source of setCurrency()

Method Source Code

'use strict';/*  w  ww .j  ava 2  s.  c o m*/
Number.prototype.setCurrency = function() {
    var re = '\\d(?=(\\d{' + (3) + '})+' + (2 > 0 ? '\\.' : '$') + ')';
    return this.toFixed(Math.max(0, ~~2)).replace(new RegExp(re, 'g'), '$&,');
};

Related

  1. toCurrency(decimals, decimal_sep, thousands_sep)
    Number.prototype.toCurrency = function(decimals, decimal_sep, thousands_sep)
       var n = this,
       c = isNaN(decimals) ? 2 : Math.abs(decimals), 
       d = decimal_sep || ',', 
       t = (typeof thousands_sep === 'undefined') ? '.' : thousands_sep, 
       sign = (n < 0) ? '-' : '',
       i = parseInt(n = Math.abs(n).toFixed(c)) + '', 
       j = ((j = i.length) > 3) ? j % 3 : 0; 
    ...
    
  2. toBillions()
    Number.prototype.toBillions = function() {
      return (this / 1000000000).toFixed(1) + 'B';
    };
    
  3. currency()
    Number.prototype.currency = function() {
        return (this.toFixed(2)+"").replace(',','.');
    
  4. currencyFormat( symbol )
    Number.prototype.currencyFormat = function( symbol )
      this.number = parseFloat( this ).toFixed(2);
      while ( /(\d+)(\d{3})/.test( this.number.toString() ) ) {
        this.number = this.number.toString().replace( /(\d+)(\d{3})/, '$1'+','+'$2' );
      return symbol + this.number;
    
  5. currencyFormat(fixed)
    Number.prototype.currencyFormat=function(fixed){
      var fixed=fixed||0;
      var str=this.toFixed(fixed);
      return str.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");