Nodejs Money Format money(decPlaces, thouSeparator, decSeparator, appendix, prefix)

Here you can find the source of money(decPlaces, thouSeparator, decSeparator, appendix, prefix)

Method Source Code

Number.prototype.money = function(decPlaces, thouSeparator, decSeparator, appendix, prefix) {
    var n = this,
        decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
        decSeparator = decSeparator == undefined ? "." : decSeparator,
        thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
        prefix = prefix || '',
        appendix = appendix || '',
        sign = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return prefix + 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) : "") + appendix;
};

Related

  1. toMoney(currency)
    Number.prototype.toMoney = function (currency) {
        currency = currency ? currency : "$";
        if (currency === "0.00") {
            return "${0}".format(currency);
        return currency + "" + this.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
    
  2. toMoney(decimals, decimal_sep, thousands_sep)
    Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep) {
       var n = this;
       var c = isNaN(decimals) ? 0 : Math.abs(decimals);
       var d = decimal_sep || '.';
       var t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
       var sign = (n < 0) ? '-' : '';
       var i = parseInt(n = Math.abs(n).toFixed(c), 10) + '';
       var 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) : '');
    ...
    
  3. toMoney(decimals, decimal_sep, thousands_sep)
    Number.prototype.toMoney = 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; 
    ...
    
  4. toMoney(decimals, decimal_sep, thousands_sep)
    Number.prototype.toMoney = 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;
    ...
    
  5. money(c, d, t)
    Number.prototype.money = function(c, d, t){
    var n = this*1000, 
        c = isNaN(c = Math.abs(c)) ? 0 : 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;
       const money = 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) : "");
    ...