number_to_currency
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (17)
- 6.1.3.1 (25)
- 6.1.7.7 (0)
- 7.0.0 (-6)
- 7.1.3.2 (-19)
- 7.1.3.4 (0)
- What's this?
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.
The currency unit and number formatting of the current locale will be used unless otherwise specified in the provided 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 - 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 %u for the currency, and %n for the number.
-
:negative_format - Sets the format for negative numbers (defaults to prepending a hyphen to the formatted number given by :format). Accepts the same fields than :format, except %n is here the absolute value of 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.506, locale: :fr) # => "1 234 567 890,51 â¬" number_to_currency('123a456') # => "$123a456" number_to_currency(-1234567890.50, negative_format: '(%u%n)') # => "($1,234,567,890.50)" number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '') # => "£1234567890,50" number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '', format: '%n %u') # => "1234567890,50 £"