Skip to content

Instantly share code, notes, and snippets.

@ewebdev
Created February 26, 2014 14:26
Show Gist options
  • Save ewebdev/9230335 to your computer and use it in GitHub Desktop.
Save ewebdev/9230335 to your computer and use it in GitHub Desktop.
function formatPrice(val, fixedDecimals, currencyCode) {
var absVal = Math.abs(val),
parts = absVal.toString().split('.'),
res;
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if (fixedDecimals) {
parts[1] = absVal.toFixed(fixedDecimals).split('.')[1];
}
res = parts.join(".");
return (val < 0 ? '-' : '') + (currencyCode || '') + res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment