method

number_to_currency

Importance_3
number_to_currency(number, options = {}) public

Formats a number into a currency string.

number_to_currency(1234567890.50)  # => "$1,234,567,890.50"
number_to_currency(1234567890.506) # => "$1,234,567,890.51"
number_to_currency("12x34")        # => "$12x34"

number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
# => "£1234567890,50"

The currency unit and number formatting of the current locale will be used unless otherwise specified via options. No currency conversion is performed. If the user is given a way to change their locale, they will also be able to change the relative value of the currency displayed with this helper. If your application will ever support multiple locales, you may want to specify a constant :locale option or consider using a library capable of currency conversion.

Options

:locale

The locale to use for formatting. Defaults to the current locale.

number_to_currency(1234567890.506, locale: :fr)
# => "1 234 567 890,51 €"
:precision

The level of precision. Defaults to 2.

number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
number_to_currency(0.456789, precision: 0)       # => "$0"
:round_mode

Specifies how rounding is performed. See BigDecimal.mode. Defaults to :default.

number_to_currency(1234567890.01, precision: 0, round_mode: :up)
# => "$1,234,567,891"
:unit

The denomination of the currency. Defaults to "$".

:separator

The decimal separator. Defaults to ".".

:delimiter

The thousands delimiter. Defaults to ",".

:format

The format for non-negative numbers. %u represents the currency, and %n represents the number. Defaults to "%u%n".

number_to_currency(1234567890.50, format: "%n %u")
# => "1,234,567,890.50 $"
:negative_format

The format for negative numbers. %u and %n behave the same as in :format, but %n represents the absolute value of the number. Defaults to the value of :format prepended with -.

number_to_currency(-1234567890.50, negative_format: "(%u%n)")
# => "($1,234,567,890.50)"
:strip_insignificant_zeros

Whether to remove insignificant zeros after the decimal separator. Defaults to false.

number_to_currency(1234567890.50, strip_insignificant_zeros: true)
# => "$1,234,567,890.5"
Show source
Register or log in to add new notes.