Here is a code snippet I use to take any number in Javascript and format it nicely to a currency, if you just want it to be a regular number, simply leave off the ‘$’ in the return line, voila!! It may look pretty garbled below, if you click on the ‘<>‘ View Source it will show you what its supposed to look like.
1 | Number.prototype.formatMoney = function (c, d, t) { |
2 | 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; |
3 | 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) : "" ); |
BTW, you can use this in any standard JavaScript call, even in Underscore templates like this:
1 | < label ><%= YourNumber.formatMoney(2, '.', ',') %></ label > |