Nodejs Currency Format toCurrency()

Here you can find the source of toCurrency()

Method Source Code

Number.prototype.toCurrency = function(){
   return "R$ " + this.format(2, 3, '.', ',');
};

Related

  1. formatCurrency(c, d, t)
    Number.prototype.formatCurrency = 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. formatCurrency(c, d, t)
    Number.prototype.formatCurrency = 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. toCurrency( noFractions, currencySymbol, decimalSeparator, thousandsSeparator )
    Number.prototype.toCurrency = function ( noFractions, currencySymbol, decimalSeparator, thousandsSeparator ) {
        var n, startAt, intLen;
        if ( currencySymbol == null ) currencySymbol = "$";
        if ( decimalSeparator == null ) decimalSeparator = ".";
        if ( thousandsSeparator == null ) thousandsSeparator = ",";
        n = this.round( noFractions ? 0 : 2, true, decimalSeparator );
        intLen = n.length - (noFractions ? 0 : 3);
        if ( (startAt = intLen % 3) == 0 ) startAt = 3;
        for ( var i = 0, len = Math.ceil( intLen / 3 ) - 1; i < len; i++ )n = n.insertAt( i * 4 + startAt, thousandsSeparator );
    ...
    
  4. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "\\") + this.withCommas();
    };
    
  5. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "") + this.withCommas();
    };
    
  6. 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) : "");
    ...
    
  7. 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');
    ...
    
  8. 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; 
    ...
    
  9. toBillions()
    Number.prototype.toBillions = function() {
      return (this / 1000000000).toFixed(1) + 'B';
    };