@@ -67,63 +67,75 @@ def number_to_phone(number, options = {})
67
67
NumberToPhoneConverter . convert ( number , options )
68
68
end
69
69
70
- # Formats a +number+ into a currency string (e.g., $13.65). You
71
- # can customize the format in the +options+ hash.
70
+ # Formats a +number+ into a currency string.
71
+ #
72
+ # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
73
+ # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
74
+ # number_to_currency("12x34") # => "$12x34"
75
+ #
76
+ # number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
77
+ # # => "£1234567890,50"
72
78
#
73
79
# The currency unit and number formatting of the current locale will be used
74
- # unless otherwise specified in the provided options. No currency conversion
75
- # is performed. If the user is given a way to change their locale, they will
80
+ # unless otherwise specified via options. No currency conversion is
81
+ # performed. If the user is given a way to change their locale, they will
76
82
# also be able to change the relative value of the currency displayed with
77
83
# this helper. If your application will ever support multiple locales, you
78
- # may want to specify a constant <tt> :locale</tt> option or consider
79
- # using a library capable of currency conversion.
84
+ # may want to specify a constant + :locale+ option or consider using a
85
+ # library capable of currency conversion.
80
86
#
81
87
# ==== Options
82
88
#
83
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
84
- # (defaults to current locale).
85
- # * <tt>:precision</tt> - Sets the level of precision (defaults
86
- # to 2).
87
- # * <tt>:round_mode</tt> - Determine how rounding is performed
88
- # (defaults to :default. See BigDecimal::mode)
89
- # * <tt>:unit</tt> - Sets the denomination of the currency
90
- # (defaults to "$").
91
- # * <tt>:separator</tt> - Sets the separator between the units
92
- # (defaults to ".").
93
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
94
- # to ",").
95
- # * <tt>:format</tt> - Sets the format for non-negative numbers
96
- # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
97
- # currency, and <tt>%n</tt> for the number.
98
- # * <tt>:negative_format</tt> - Sets the format for negative
99
- # numbers (defaults to prepending a hyphen to the formatted
100
- # number given by <tt>:format</tt>). Accepts the same fields
101
- # than <tt>:format</tt>, except <tt>%n</tt> is here the
102
- # absolute value of the number.
103
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
104
- # insignificant zeros after the decimal separator (defaults to
105
- # +false+).
89
+ # [+:locale+]
90
+ # The locale to use for formatting. Defaults to the current locale.
106
91
#
107
- # ==== Examples
92
+ # number_to_currency(1234567890.506, locale: :fr)
93
+ # # => "1 234 567 890,51 €"
94
+ #
95
+ # [+:precision+]
96
+ # The level of precision. Defaults to 2.
97
+ #
98
+ # number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
99
+ # number_to_currency(0.456789, precision: 0) # => "$0"
100
+ #
101
+ # [+:round_mode+]
102
+ # Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
103
+ # +:default+.
104
+ #
105
+ # number_to_currency(1234567890.01, precision: 0, round_mode: :up)
106
+ # # => "$1,234,567,891"
107
+ #
108
+ # [+:unit+]
109
+ # The denomination of the currency. Defaults to <tt>"$"</tt>.
110
+ #
111
+ # [+:separator+]
112
+ # The decimal separator. Defaults to <tt>"."</tt>.
113
+ #
114
+ # [+:delimiter+]
115
+ # The thousands delimiter. Defaults to <tt>","</tt>.
116
+ #
117
+ # [+:format+]
118
+ # The format for non-negative numbers. <tt>%u</tt> represents the currency,
119
+ # and <tt>%n</tt> represents the number. Defaults to <tt>"%u%n"</tt>.
120
+ #
121
+ # number_to_currency(1234567890.50, format: "%n %u")
122
+ # # => "1,234,567,890.50 $"
123
+ #
124
+ # [+:negative_format+]
125
+ # The format for negative numbers. <tt>%u</tt> and <tt>%n</tt> behave the
126
+ # same as in +:format+, but <tt>%n</tt> represents the absolute value of
127
+ # the number. Defaults to the value of +:format+ prepended with <tt>-</tt>.
128
+ #
129
+ # number_to_currency(-1234567890.50, negative_format: "(%u%n)")
130
+ # # => "($1,234,567,890.50)"
131
+ #
132
+ # [+:strip_insignificant_zeros+]
133
+ # Whether to remove insignificant zeros after the decimal separator.
134
+ # Defaults to false.
135
+ #
136
+ # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
137
+ # # => "$1,234,567,890.5"
108
138
#
109
- # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
110
- # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
111
- # number_to_currency(1234567890.506, precision: 3) # => "$1,234,567,890.506"
112
- # number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
113
- # number_to_currency('123a456') # => "$123a456"
114
- #
115
- # number_to_currency(-0.456789, precision: 0)
116
- # # => "$0"
117
- # number_to_currency(-1234567890.50, negative_format: '(%u%n)')
118
- # # => "($1,234,567,890.50)"
119
- # number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '')
120
- # # => "£1234567890,50"
121
- # number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '', format: '%n %u')
122
- # # => "1234567890,50 £"
123
- # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
124
- # # => "$1,234,567,890.5"
125
- # number_to_currency(1234567890.50, precision: 0, round_mode: :up)
126
- # # => "$1,234,567,891"
127
139
def number_to_currency ( number , options = { } )
128
140
NumberToCurrencyConverter . convert ( number , options )
129
141
end
0 commit comments