trollolodn

#javascript #formatting currency

Jan 22nd, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Number/currency formatting. Use prototype function for int var.
  2. Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) {
  3.     var n = this,
  4.     decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
  5.     decSeparator = decSeparator == undefined ? "." : decSeparator,
  6.     thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
  7.     sign = n < 0 ? "-" : "",
  8.     i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
  9.     j = (j = i.length) > 3 ? j % 3 : 0;
  10.     return 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) : "");
  11. };
Advertisement
Add Comment
Please, Sign In to add comment