Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lpcuong2106/0e4dbfaea7257a550de44772ff50f981 to your computer and use it in GitHub Desktop.
Save lpcuong2106/0e4dbfaea7257a550de44772ff50f981 to your computer and use it in GitHub Desktop.
Regular Expression to Format Currency In Javascript
function formatCurrency(amount)
{
//truncate the amount to 0 decimals
//for every digit that is followed by 3 digits and a word boundary
//add a comma
amount = amount.toFixed(0).replace(/(\d)(?=(\d{3})+\b)/g, "$1,");
return amount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment