Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function formatCurrency(number) {
var numberStr = number.toString();
var thousandsMatcher = /(\d+)(\d{3})$/;
var thousandsAndRest = thousandsMatcher.exec(numberStr);
if (!thousandsAndRest) return numberStr;
return thousandsAndRest[1].replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + thousandsAndRest[2];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment