Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created August 5, 2016 09:46
Show Gist options
  • Select an option

  • Save SteGriff/80ef7bb097c0601befa5fcc84ebab1b6 to your computer and use it in GitHub Desktop.

Select an option

Save SteGriff/80ef7bb097c0601befa5fcc84ebab1b6 to your computer and use it in GitHub Desktop.
Adapted version of formatMoney
//From http://www.josscrowcroft.com/2011/code/format-unformat-money-currency-javascript/
// Extend the default Number object with a formatMoney() method:
// usage: someVar.formatMoney(decimalPlaces = 2)
Number.prototype.formatMoney = function (places) {
places = !isNaN(places = Math.abs(places)) ? places : 2;
thousand = ",";
decimal = ".";
var number = this,
i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
};
@SteGriff

SteGriff commented Aug 5, 2016

Copy link
Copy Markdown
Author

Dropped the currency symbol and negative symbol, and defaulted the thousand and decimal seperators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment