Method: ActionView::Helpers::NumberHelper#number_to_currency

Defined in:
lib/action_view/helpers/number_helper.rb

#number_to_currency(number, options = {}) ⇒ Object

Formats a number into a currency string (e.g., $13.65). You can customize the format in the options hash.

Options

  • :locale - Sets the locale to be used for formatting (defaults to current locale).

  • :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 for non-negative numbers (defaults to “%u%n”).

    Fields are <tt>%u</tt> for the currency, and <tt>%n</tt>
    for the number.
    
  • :negative_format - Sets the format for negative numbers (defaults to prepending

    an hyphen to the formatted number given by <tt>:format</tt>).
    Accepts the same fields than <tt>:format</tt>, except
    <tt>%n</tt> is here the absolute value of the number.
    
  • :raise - If true, raises InvalidNumberError when the argument is invalid.

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.506, :locale => :fr)   # => 1 234 567 890,51 €
number_to_currency("123a456")                        # => $123a456

number_to_currency("123a456", :raise => true)        # => InvalidNumberError

number_to_currency(-1234567890.50, :negative_format => "(%u%n)")
# => ($1,234,567,890.50)
number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")
# => &pound;1234567890,50
number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
# => 1234567890,50 &pound;


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/action_view/helpers/number_helper.rb', line 124

def number_to_currency(number, options = {})
  return unless number

  options.symbolize_keys!

  defaults  = I18n.translate(:'number.format', :locale => options[:locale], :default => {})
  currency  = I18n.translate(:'number.currency.format', :locale => options[:locale], :default => {})
  currency[:negative_format] ||= "-" + currency[:format] if currency[:format]

  defaults  = DEFAULT_CURRENCY_VALUES.merge(defaults).merge!(currency)
  defaults[:negative_format] = "-" + options[:format] if options[:format]
  options   = defaults.merge!(options)

  unit      = options.delete(:unit)
  format    = options.delete(:format)

  if number.to_f < 0
    format = options.delete(:negative_format)
    number = number.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
  end

  begin
    value = number_with_precision(number, options.merge(:raise => true))
    format.gsub(/%n/, value).gsub(/%u/, unit).html_safe
  rescue InvalidNumberError => e
    if options[:raise]
      raise
    else
      formatted_number = format.gsub(/%n/, e.number).gsub(/%u/, unit)
      e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number
    end
  end

end