Created
August 5, 2016 09:46
-
-
Save SteGriff/80ef7bb097c0601befa5fcc84ebab1b6 to your computer and use it in GitHub Desktop.
Adapted version of formatMoney
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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) : ""); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dropped the currency symbol and negative symbol, and defaulted the thousand and decimal seperators.