number_to_currency(number, options = {}) public

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 £
Show source
Register or log in to add new notes.
August 29, 2008
2 thanks

Brazilian Real (R$ 1.200,95)

helper:

 def number_to_currency_br(number)
   number_to_currency(number, :unit => "R$ ", :separator => ",", :delimiter => ".")
 end
October 20, 2008
1 thank

number_to_euro

in small cells:

  12-->
  12def number_to_euro(amount)
    number_to_currency(amount,:unit=>'').gsub(' ',nbsp)
  end