Skip to content

Commit 9acf9e6

Browse files
Clean up AS::NumberHelper#number_to_currency doc [ci-skip]
1 parent 6be13c8 commit 9acf9e6

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

activesupport/lib/active_support/number_helper.rb

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,63 +67,75 @@ def number_to_phone(number, options = {})
6767
NumberToPhoneConverter.convert(number, options)
6868
end
6969

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"
7278
#
7379
# 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
7682
# also be able to change the relative value of the currency displayed with
7783
# 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.
8086
#
8187
# ==== Options
8288
#
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.
10691
#
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"
108138
#
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: '&pound;', separator: ',', delimiter: '')
120-
# # => "&pound;1234567890,50"
121-
# number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
122-
# # => "1234567890,50 &pound;"
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"
127139
def number_to_currency(number, options = {})
128140
NumberToCurrencyConverter.convert(number, options)
129141
end

0 commit comments

Comments
 (0)