Skip to content

Instantly share code, notes, and snippets.

@lenagroeger
Created August 28, 2012 19:06
Show Gist options
  • Save lenagroeger/3502562 to your computer and use it in GitHub Desktop.
Save lenagroeger/3502562 to your computer and use it in GitHub Desktop.
helper to turn $5,758,937 into $5M
def fmt_stat(stat, prefix = "$")
stat = case stat
when -999999999999..-10000 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when -9999..9999 then number_with_delimiter(stat)
when 10000..999999999999 then number_to_human(stat, :units => {:million => "M", :thousand => "K", :billion => "B"}).gsub(/ /,'')
when nil then number_with_delimiter(0)
else stat
end
stat = stat === "N/A" ? stat : "#{prefix}#{stat}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment