Created
November 19, 2012 19:19
-
-
Save danmartens/4113025 to your computer and use it in GitHub Desktop.
Format Money in Coffeescript
This file contains 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
# Inspired by http://stackoverflow.com/a/149099/1649199 | |
Number::formatMoney = (t=',', d='.', c='$') -> | |
n = this | |
s = if n < 0 then "-#{c}" else c | |
i = Math.abs(n).toFixed(2) | |
j = (if (j = i.length) > 3 then j % 3 else 0) | |
s += i.substr(0, j) + t if j | |
return s + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment