Created
April 16, 2013 07:15
-
-
Save jsguy/5393995 to your computer and use it in GitHub Desktop.
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
// 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