Nodejs Money Format toMoney(decimals, decimal_sep, thousands_sep)

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

Method Source Code

Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep) {
   var n = this;//from  w w  w  .  ja  v  a  2 s.  com
   var c = isNaN(decimals) ? 0 : Math.abs(decimals);
   var d = decimal_sep || '.';
   var t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
   var sign = (n < 0) ? '-' : '';

   // absolute value of the integer part of the number and convert to a string
   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) : '');
};

var N = {};

N.getCleanValue = function(name) {
  var v = $('input#' + name).val();
  v = v.replace(/^\./, '0.');
  v = v.replace(/^[^0-9\-]*/, '');
  v = v.replace(/\D*$/, '');
  v = v.replace(/,/g, '');
  return v;
};

Related

  1. 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;
    ...
    
  2. 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) : "");
    ...
    
  3. toMoney()
    Number.prototype.toMoney = function() {
      return '$' + this.toFixed(2);
    };
    
  4. 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) : "");
    ...
    
  5. 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,");
    
  6. 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; 
    ...
    
  7. 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;
    ...
    
  8. 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) : "");
    ...
    
  9. money(decPlaces, thouSeparator, decSeparator, appendix, prefix)
    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)) + "",
    ...