Nodejs Currency Format toCurrency( noFractions, currencySymbol, decimalSeparator, thousandsSeparator )

Here you can find the source of toCurrency( noFractions, currencySymbol, decimalSeparator, thousandsSeparator )

Method Source Code

/**//from  ww  w .  j ava2 s  .  c  om
 * Estende la classe Number
 *
 * @author          =undo= <info@wpxtre.me>
 * @copyright       Copyright (C) 2012-2013 wpXtreme Inc. All Rights Reserved.
 * @date            09/12/11
 * @version         1.0
 *
 * @uses            String+Insertion.js
 *
 */

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 );
    return currencySymbol + n;
};

Related

  1. formatCurrency(num)
    function formatCurrency(num) {
        num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num))
        num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10)
    ...
    
  2. ToCurrencyWithSymbol(ccyCode, rounder)
    Number.prototype.ToCurrencyWithSymbol = function (ccyCode, rounder) {
        var symbol = ccyCode.ConvertToCurrencySymbol();
        aDigits = this.toFixed(rounder).split(".");
        aDigits[0] = aDigits[0].split("").reverse().join("")
                                        .replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join("");
        return symbol + aDigits.join(".");
    
  3. formatCurrency()
    Number.prototype.formatCurrency = function()
      return '$' + this.toFixed(2);
    
  4. 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) : "");
    ...
    
  5. 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) : "");
    };
    
  6. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "\\") + this.withCommas();
    };
    
  7. toCurrency()
    Number.prototype.toCurrency = function() {
      return (arguments[0] ? arguments[0] : "") + this.withCommas();
    };
    
  8. toCurrency()
    Number.prototype.toCurrency = function(){
      return "R$ " + this.format(2, 3, '.', ',');
    };
    
  9. 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) : "");
    ...