Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created May 2, 2013 13:04
Show Gist options
  • Save rmosolgo/5502031 to your computer and use it in GitHub Desktop.
Save rmosolgo/5502031 to your computer and use it in GitHub Desktop.
nice_money = (amount, options={}) ->
# Props to akmiller for original code -- I'm using it all the time though so hosting it here.
full_label = options.full_label || false
if d3?
if 1000 >= amount
label = ""
money = "#{d3.format("0,r")(d3.round(amount,0))}"
else if 1000000 > amount >= 1000
label = (if full_label then "Thousand" else "K" )
money = "#{d3.round((amount/1000),0)}"
else if 1000000000 > amount >= 1000000
label = (if full_label then "Million" else "M")
money = "#{d3.round((amount/1000000),1)}"
else if amount >= 1000000000
label = (if full_label then "Billion" else "B")
money = "#{d3.format("0,r")(d3.round((amount/1000000000),2))}"
else
label = ""
money = amount
"#{money} #{label}"
else
console.log "nice_money requires D3"
amount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment