Skip to content

Instantly share code, notes, and snippets.

@danmartens
Created November 19, 2012 19:19
Show Gist options
  • Save danmartens/4113025 to your computer and use it in GitHub Desktop.
Save danmartens/4113025 to your computer and use it in GitHub Desktop.
Format Money in Coffeescript
# 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