Javascript Number formatMoney(c, d, t)

Description

Javascript Number formatMoney(c, d, t)


Number.prototype.formatMoney = function(c, d, t){
var n = this,/*from  w ww  .j  a v a  2 s  . c  o  m*/
    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) : "");
 };

Javascript Number formatMoney(c, d, t)

function formatDate(date){
  if(typeof(date) == 'string'){
    date = new Date(Date.parse(date));
  }/*from   ww  w.j a v  a  2  s.  c om*/
  return date.toDateString();
}
    
Number.prototype.formatMoney = 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) : "");
};

Javascript Number formatMoney(c, d, t)

Number.prototype.formatMoney = function(c, d, t){

   var n = this;//www  .ja v  a 2 s.  c om
   var c = isNaN(c = Math.abs(c)) ? 2 : c;
   var d = d == undefined ? "," : d;
   var t = t == undefined ? "." : t;
   var s = n < 0 ? "-" : "";
   var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "";
   var 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) : "");
};

var x = (2000).formatMoney(/*'casas decimais', 'separador virgula', 'separador ponto'*/);

console.log(x);

Javascript Number formatMoney(c, d, t)

Number.prototype.formatMoney = 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) : "");
 };/*from w w  w . j a v a  2  s  .c  o  m*/

function prettifyNumber(amount, abbrev) {
  var divisor = 100;
  var magnitude = 'Hundred';
  
  if (abbrev == 'M') {
    divisor = 1000000;
    magnitude = 'Million';
  } else if (abbrev == 'B') {
    divisor = 1000000000;
    magnitude = 'Billion';
  } else if (abbrev == 'T') {
    divisor = 1000000000000;
    magnitude = 'Trillion';
  }

  return '$' + (Math.round(amount/divisor * 100)/100).formatMoney(2, '.', ',') + ' ' + magnitude;
}



PreviousNext

Related