Nodejs Currency Format toCurrency(decimals, decimal_sep, thousands_sep)

Here you can find the source of toCurrency(decimals, decimal_sep, thousands_sep)

Method Source Code

Number.prototype.toCurrency = function(decimals, decimal_sep, thousands_sep)
{ 
   var n = this,//  w w  w . jav a2 s.  c  o m
   c = isNaN(decimals) ? 2 : Math.abs(decimals), //if decimal is zero we must take it, it means user does not want to show any decimal
   d = decimal_sep || ',', //if no decimal separetor is passed we use the comma as default decimal separator (we MUST use a decimal separator)

   /*
   according to [http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function]
   the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
   rather than doing value === undefined.
   */   
   t = (typeof thousands_sep === 'undefined') ? '.' : thousands_sep, //if you don't want ot use a thousands separator you can pass empty string as thousands_sep value

   sign = (n < 0) ? '-' : '',

   //extracting the absolute value of the integer part of the number and converting to string
   i = parseInt(n = Math.abs(n).toFixed(c)) + '', 

   j = ((j = i.length) > 3) ? j % 3 : 0; 
   return sign + (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) : ''); 
}

Related

  1. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "\\") + this.withCommas();
    };
    
  2. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "") + this.withCommas();
    };
    
  3. toCurrency()
    Number.prototype.toCurrency = function(){
      return "R$ " + this.format(2, 3, '.', ',');
    };
    
  4. toCurrency(c, d, t)
    Number.prototype.toCurrency = 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. toCurrency(decimalPlaces)
    Number.prototype.toCurrency = function(decimalPlaces){                               
      decimalPlaces = (decimalPlaces === undefined) ? 2 : decimalPlaces;
      var nStr = (decimalPlaces != null) ? this.toFixed(decimalPlaces) : this.toString();
      var x = nStr.split('.');
      var x1 = x[0];
      var x2 = x.length > 1 ? ',' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    ...
    
  6. toBillions()
    Number.prototype.toBillions = function() {
      return (this / 1000000000).toFixed(1) + 'B';
    };
    
  7. currency()
    Number.prototype.currency = function() {
        return (this.toFixed(2)+"").replace(',','.');
    
  8. 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;
    
  9. 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,");