Skip to content

Instantly share code, notes, and snippets.

@jsguy
Created April 16, 2013 07:15
Show Gist options
  • Save jsguy/5393995 to your computer and use it in GitHub Desktop.
Save jsguy/5393995 to your computer and use it in GitHub Desktop.
// Nicely format a number as money, including germanic style - original from: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
// eg: console.log(formatMoney(123456789.12345, ',', '.'));
var formatMoney = function(value, decimalPoint, thousandSeperator) {
var sign = value < 0 ? "-" : "",
number = parseInt(value = Math.abs(+value || 0).toFixed(2), 10) + "",
splitThousand = (splitThousand = number.length) > 3 ? splitThousand % 3 : 0;
decimalPoint = decimalPoint == undefined ? "." : decimalPoint;
thousandSeperator = thousandSeperator == undefined ? "," : thousandSeperator;
return sign + (splitThousand ? number.substr(0, splitThousand) + thousandSeperator : "") + number.substr(splitThousand).replace(/(\d{3})(?=\d)/g, "$1" + thousandSeperator) + (2 ? decimalPoint + Math.abs(value - number).toFixed(2).slice(2) : "");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment