Nodejs Money Format formatMoney(places, symbol, thousand, decimal)

Here you can find the source of formatMoney(places, symbol, thousand, decimal)

Method Source Code

/**//  www.  jav  a2  s. com
 * Created by cfinnegan on 30/10/15.
 */
// Extend the default Number object with a formatMoney() method:
// usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)
// defaults: (2, "$", ",", ".")
Number.prototype.formatMoney = function(places, symbol, thousand, decimal) {
    places = !isNaN(places = Math.abs(places)) ? places : 2;
    symbol = symbol !== undefined ? symbol : "$";
    thousand = thousand || ",";
    decimal = decimal || ".";
    var number = this,
        negative = number < 0 ? "-" : "",
        i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
};

Related

  1. formatMoney(decPlaces, thouSeparator, decSeparator,
    Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator,
        currencySymbol) {
      var n = this, decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2
          : decPlaces, decSeparator = decSeparator == undefined ? "."
          : decSeparator, thouSeparator = thouSeparator == undefined ? ","
          : thouSeparator, currencySymbol = currencySymbol == undefined ? "$"
          : currencySymbol, sign = n < 0 ? "-" : "", i = parseInt(n = Math
          .abs(+n || 0).toFixed(decPlaces))
          + "", j = (j = i.length) > 3 ? j % 3 : 0;
    ...
    
  2. formatMoney(decPlaces, thouSeparator, decSeparator, currencySymbol)
    Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator, currencySymbol) {
        decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces;
        decSeparator = decSeparator == undefined ? "." : decSeparator;
        thouSeparator = thouSeparator == undefined ? "," : thouSeparator;
        currencySymbol = currencySymbol == undefined ? "$" : currencySymbol;
        var n = this,
            sign = n < 0 ? "-" : "",
            i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
    ...
    
  3. formatMoney(decimalPart, currencySymbol, decimalDivider, thousandDivider)
    Number.prototype.formatMoney = function(decimalPart, currencySymbol, decimalDivider, thousandDivider){
        var n = this,
            c = isNaN(decimalPart = Math.abs(decimalPart)) ? 2 : decimalPart,
            d = decimalDivider == undefined ? "." : decimalDivider,
            t = thousandDivider == undefined ? " " : thousandDivider,
            s = n < 0 ? "-" : "",
            i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
            j = (j = i.length) > 3 ? j % 3 : 0,
            a = currencySymbol == (undefined || "")? "" : " "+currencySymbol;
    ...
    
  4. formatMoney(groupsDelimeter, fractionLength, fractionDelimeter)
    Number.prototype.formatMoney = function(groupsDelimeter, fractionLength, fractionDelimeter){
      var n = this,
        c = isNaN(fractionLength = Math.abs(fractionLength)) ? 2 : fractionLength,
        d = fractionDelimeter == undefined ? "." : fractionDelimeter,
        t = groupsDelimeter == undefined ? "," : groupsDelimeter,
        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. formatMoney(places, symbol, thousand)
    Number.prototype.formatMoney = function (places, symbol, thousand) {
        places = !isNaN(places = Math.abs(places)) ? places : 2;
        symbol = symbol !== undefined ? symbol : "$";
        thousand = thousand || ",";
        var number = this,
            negative = number < 0 ? "-" : "",
            i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
        return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand);
    ...
    
  6. formatMoney(places, symbol, thousand, decimal)
    Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
      places = !isNaN(places = Math.abs(places)) ? places : 2;
      symbol = symbol !== undefined ? symbol : "$";
      thousand = thousand || ",";
      decimal = decimal || ".";
      let number = this,
        negative = number < 0 ? "-" : "",
        i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    ...
    
  7. formatMoney2(c, d, t)
    Number.prototype.formatMoney2 = 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) : "");
    ...
    
  8. toMoney()
    Number.prototype.toMoney = function() {
      return '$' + this.toFixed(2);
    };
    
  9. toMoney(c, d, t)
    Number.prototype.toMoney = 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) : "");
    ...