Number.Format Money - JSFromHell.com: JavaScript Repository

Edit
Working with money formatting in javascript lately and found there is no library out there that handle this, but there are a number of code snippets flowing around to do it, and this javascript number formatter is a compact and nice one. The only problem is that it used "." as the default thousand separator and "," as the decimal separator. So I make some small change and it becomes this.

//+ Modified from Jonas Raoni Soares Silva
//@ http://jsfromhell.com/number/fmt-money [rev. #2]

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) : "");
};"



usage : 
(123456.789).formatMoney(0);
(123456.789).formatMoney(2);
(123456.789).formatMoney(3,".",",");

Original : Number.Format Money - JSFromHell.com: JavaScript Repository

Number.Format Money - JSFromHell.com: JavaScript Repository Number.Format Money - JSFromHell.com: JavaScript Repository Reviewed by DF on 9:17:00 PM Rating: 5
©DF. Powered by Blogger.