module ActiveSupport::NumberHelper
def number_to_currency(number, options = {})
# => "$1,234,567,890.5"
number_to_currency(1234567890.50, strip_insignificant_zeros: true)
Defaults to false.
Whether to remove insignificant zeros after the decimal separator.
[+:strip_insignificant_zeros+]
# => "($1,234,567,890.50)"
number_to_currency(-1234567890.50, negative_format: "(%u%n)")
the number. Defaults to the value of +:format+ prepended with -.
same as in +:format+, but %n represents the absolute value of
The format for negative numbers. %u and %n behave the
[+:negative_format+]
# => "1,234,567,890.50 $"
number_to_currency(1234567890.50, format: "%n %u")
and %n represents the number. Defaults to "%u%n".
The format for non-negative numbers. %u represents the currency,
[+:format+]
The thousands delimiter. Defaults to ",".
[+:delimiter+]
The decimal separator. Defaults to ".".
[+:separator+]
The denomination of the currency. Defaults to "$".
[+:unit+]
# => "$1,234,567,891"
number_to_currency(1234567890.01, precision: 0, round_mode: :up)
+:default+.
Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
[+:round_mode+]
number_to_currency(0.456789, precision: 0) # => "$0"
number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
The level of precision. Defaults to 2.
[+:precision+]
# => "1 234 567 890,51 €"
number_to_currency(1234567890.506, locale: :fr)
The locale to use for formatting. Defaults to the current locale.
[+:locale+]
==== Options
library capable of currency conversion.
may want to specify a constant +:locale+ option or consider using a
this helper. If your application will ever support multiple locales, you
also be able to change the relative value of the currency displayed with
performed. If the user is given a way to change their locale, they will
unless otherwise specified via options. No currency conversion is
The currency unit and number formatting of the current locale will be used
# => "£1234567890,50"
number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
number_to_currency("12x34") # => "$12x34"
number_to_currency(1234567890.506) # => "$1,234,567,890.51"
number_to_currency(1234567890.50) # => "$1,234,567,890.50"
Formats a +number+ into a currency string.
def number_to_currency(number, options = {}) NumberToCurrencyConverter.convert(number, options) end