Created
September 5, 2012 19:15
-
-
Save rwjblue/3642861 to your computer and use it in GitHub Desktop.
format_currency Method
This file contains 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
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