instance method
number_to_currency
Ruby on Rails 2.2.3
Since v2.2.3Signature
number_to_currency(number, options = {})
Formats a number into a currency string (e.g., $13.65). You can customize the format in the options hash.
Options
-
:precision- Sets the level of precision (defaults to 2). -
:unit- Sets the denomination of the currency (defaults to “$”). -
:separator- Sets the separator between the units (defaults to “.”). -
:delimiter- Sets the thousands delimiter (defaults to “,”). -
:format- Sets the format of the output string (defaults to “%u%n”). The field types are:%u The currency unit %n The number
Examples
number_to_currency(1234567890.50) # => $1,234,567,890.50 number_to_currency(1234567890.506) # => $1,234,567,890.51 number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506 number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "") # => £1234567890,50 number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u") # => 1234567890,50 £
Parameters
-
numberreq -
optionsopt = {}
Source
# File actionpack/lib/action_view/helpers/number_helper.rb, line 71
def number_to_currency(number, options = {})
options.symbolize_keys!
defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :raise => true) rescue {}
defaults = defaults.merge(currency)
precision = options[:precision] || defaults[:precision]
unit = options[:unit] || defaults[:unit]
separator = options[:separator] || defaults[:separator]
delimiter = options[:delimiter] || defaults[:delimiter]
format = options[:format] || defaults[:format]
separator = '' if precision == 0
begin
format.gsub(/%n/, number_with_precision(number,
:precision => precision,
:delimiter => delimiter,
:separator => separator)
).gsub(/%u/, unit)
rescue
number
end
end
Defined in actionpack/lib/action_view/helpers/number_helper.rb line 71
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::NumberHelper