Skip to content

Instantly share code, notes, and snippets.

@slav123
Created March 19, 2013 22:50
Show Gist options
  • Save slav123/5200877 to your computer and use it in GitHub Desktop.
Save slav123/5200877 to your computer and use it in GitHub Desktop.
money format coffee script
Number::formatMoney = (decimalPlaces, decimalChar, thousandsChar) ->
n = this
c = decimalPlaces
d = decimalChar
t = thousandsChar
c = (if isNaN(c = Math.abs(c)) then 2 else c)
d = (if d is undefined then "." else d)
t = (if t is undefined then "," else t)
s = (if n < 0 then "-" else "")
i = parseInt(n = Math.abs(+n or 0).toFixed(c)) + ""
j = (if (j = i.length) > 3 then j % 3 else 0)
s + (if j then i.substr(0, j) + t else "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (if c then d + Math.abs(n - i).toFixed(c).slice(2) else "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment