method

number_to_currency

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

  • :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;

6Notes

Brazilian Real (R$ 1.200,95)

neves · Aug 29, 20086 thanks

helper: def number_to_currency_br(number) number_to_currency(number, :unit => "R$ ", :separator => ",", :delimiter => ".") end

number_to_euro

grosser · Oct 20, 20083 thanks

in small cells:

12  
€
-->
12 €


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

Use this in controllers

georges · Jan 15, 20103 thanks

Sometimes you're gonna need this in controllers. Just put this in the controller:

include ActionView::Helpers::NumberHelper

How to change format automatically depending on locale...

stevo · Oct 4, 20112 thanks

... without passing locale option.

In your application_helper.rb (or in other helper) place following code:

def number_to_currency(number, options = {})
options[:locale] ||= I18n.locale
super(number, options)
end

Then, in your locale files: en-GB: number: currency: format: format: "%n %u" unit: "USD"

And that is it :)

Bangladeshi Taka (BDT 1,200.95)

nishat · Nov 19, 20141 thank

==== Code example

def to_bdt(amount)
number_to_currency(amount, :unit => "BDT ", :separator => ".", :delimiter => ",")
end

If you happen to face some weird rounding issue...

stevo · Oct 24, 2012

i.e.

helper.number_to_currency(187) => "190 kr"

check out your... translations! Especially 'significant' key... In my case it was

number: currency: format: significant: 'false'

that broke rounding. It should have been

number: currency: format: significant: ! 'false'

And now it works perfectly

helper.number_to_currency(187) => "187 kr"