Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Created September 5, 2012 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwjblue/3642861 to your computer and use it in GitHub Desktop.
Save rwjblue/3642861 to your computer and use it in GitHub Desktop.
format_currency Method
def format_currency(input, options = {:currency_symbol => '$', :delimiter => ',', :separator => '.', :precision => 2})
input ||= 0
number = "%01.#{options[:precision]}f" % input.to_d.round(options[:precision]).to_s("F")
parts = number.to_s.to_str.split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
options[:currency_symbol] + parts.join(options[:separator])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment