Created
March 19, 2013 22:50
-
-
Save slav123/5200877 to your computer and use it in GitHub Desktop.
money format coffee script
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
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