Skip to content

Instantly share code, notes, and snippets.

@humrochagf
Created May 16, 2015 00:39
Show Gist options
  • Save humrochagf/62b80a85b769fda67940 to your computer and use it in GitHub Desktop.
Save humrochagf/62b80a85b769fda67940 to your computer and use it in GitHub Desktop.
Javascript currency formating with comma decimal separation
function number_to_money(num, currency) {
currency = typeof currency !== 'undefined' ? currency : '$';
return currency + ' ' + num.toFixed(2).replace('.', ',').replace(/(?=(\d{3})+\,)/g, '.').replace(/^\./,'');
};
var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67];
for (var i = 0; i < numbers.length; i++) {
document.write(number_to_money(numbers[i], 'R$') + '<br />');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment