Skip to content

Instantly share code, notes, and snippets.

@leftis
Last active August 29, 2015 14:06
Show Gist options
  • Save leftis/0dd86f04f9698bcd246e to your computer and use it in GitHub Desktop.
Save leftis/0dd86f04f9698bcd246e to your computer and use it in GitHub Desktop.
Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep)
{
var n = this,
c = isNaN(decimals) ? 0 : 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;
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) : '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment