Skip to content

Instantly share code, notes, and snippets.

@olveirap
Created February 15, 2017 04:02
Show Gist options
  • Save olveirap/7271ae2459ef17745f266def96a7fe3b to your computer and use it in GitHub Desktop.
Save olveirap/7271ae2459ef17745f266def96a7fe3b to your computer and use it in GitHub Desktop.
Price formatter function
//http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
function formatPrice(aPrice){
var n = aPrice;
var i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(2)));
var j = (j = i.length) > 3 ? j % 3 : 0;
return (j ? i.substr(0, j) + "." : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ".") + (2 ? "," + Math.abs(n - i).toFixed(2).slice(2) : "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment