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%u
for the currency, and%n
for the number. -
:negative_format
- Sets the format for negative numbers (defaults to prepending an hyphen to the formatted number given by:format
). Accepts the same fields than:format
, except%n
is here the absolute value of the number. -
:raise
- If true, raisesInvalidNumberError
when the argument is invalid.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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: "R$" , separator: "," , delimiter: "" ) # => R$1234567890,50 number_to_currency( 1234567890 . 50 , unit: "R$" , separator: "," , delimiter: "" , format: "%n %u" ) # => 1234567890,50 R$ |
Please login to continue.