Skip to content

Commit 1d02d9b

Browse files
Clean up AS::NumberHelper#number_to_percentage doc [ci-skip]
1 parent 9acf9e6 commit 1d02d9b

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

activesupport/lib/active_support/number_helper.rb

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -140,42 +140,64 @@ def number_to_currency(number, options = {})
140140
NumberToCurrencyConverter.convert(number, options)
141141
end
142142

143-
# Formats a +number+ as a percentage string (e.g., 65%). You can
144-
# customize the format in the +options+ hash.
143+
# Formats +number+ as a percentage string.
144+
#
145+
# number_to_percentage(100) # => "100.000%"
146+
# number_to_percentage("99") # => "99.000%"
147+
# number_to_percentage("99x") # => "99x%"
148+
#
149+
# number_to_percentage(12345.6789, delimiter: ".", separator: ",", precision: 2)
150+
# # => "12.345,68%"
145151
#
146152
# ==== Options
147153
#
148-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
149-
# (defaults to current locale).
150-
# * <tt>:precision</tt> - Sets the precision of the number
151-
# (defaults to 3). Keeps the number's precision if +nil+.
152-
# * <tt>:round_mode</tt> - Determine how rounding is performed
153-
# (defaults to :default. See BigDecimal::mode)
154-
# * <tt>:significant</tt> - If +true+, precision will be the number
155-
# of significant_digits. If +false+, the number of fractional
156-
# digits (defaults to +false+).
157-
# * <tt>:separator</tt> - Sets the separator between the
158-
# fractional and integer digits (defaults to ".").
159-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
160-
# to "").
161-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
162-
# insignificant zeros after the decimal separator (defaults to
163-
# +false+).
164-
# * <tt>:format</tt> - Specifies the format of the percentage
165-
# string The number field is <tt>%n</tt> (defaults to "%n%").
154+
# [+:locale+]
155+
# The locale to use for formatting. Defaults to the current locale.
166156
#
167-
# ==== Examples
157+
# number_to_percentage(1000, locale: :fr)
158+
# # => "1000,000%"
159+
#
160+
# [+:precision+]
161+
# The level of precision, or +nil+ to preserve +number+'s precision.
162+
# Defaults to 2.
163+
#
164+
# number_to_percentage(12.3456789, precision: 4) # => "12.3457%"
165+
# number_to_percentage(99.999, precision: 0) # => "100%"
166+
# number_to_percentage(99.999, precision: nil) # => "99.999%"
167+
#
168+
# [+:round_mode+]
169+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
170+
# +:default+.
171+
#
172+
# number_to_percentage(12.3456789, precision: 4, round_mode: :down)
173+
# # => "12.3456%"
174+
#
175+
# [+:significant+]
176+
# Whether +:precision+ should be applied to significant digits instead of
177+
# fractional digits. Defaults to false.
178+
#
179+
# number_to_percentage(12345.6789) # => "12345.679%"
180+
# number_to_percentage(12345.6789, significant: true) # => "12300%"
181+
# number_to_percentage(12345.6789, precision: 2) # => "12345.68%"
182+
# number_to_percentage(12345.6789, precision: 2, significant: true) # => "12000%"
183+
#
184+
# [+:separator+]
185+
# The decimal separator. Defaults to <tt>"."</tt>.
186+
#
187+
# [+:delimiter+]
188+
# The thousands delimiter. Defaults to <tt>","</tt>.
189+
#
190+
# [+:strip_insignificant_zeros+]
191+
# Whether to remove insignificant zeros after the decimal separator.
192+
# Defaults to false.
193+
#
194+
# [+:format+]
195+
# The format of the output. <tt>%n</tt> represents the number. Defaults to
196+
# <tt>"%n%"</tt>.
197+
#
198+
# number_to_percentage(100, format: "%n %")
199+
# # => "100.000 %"
168200
#
169-
# number_to_percentage(100) # => "100.000%"
170-
# number_to_percentage('98') # => "98.000%"
171-
# number_to_percentage(100, precision: 0) # => "100%"
172-
# number_to_percentage(1000, delimiter: '.', separator: ',') # => "1.000,000%"
173-
# number_to_percentage(302.24398923423, precision: 5) # => "302.24399%"
174-
# number_to_percentage(1000, locale: :fr) # => "1000,000%"
175-
# number_to_percentage(1000, precision: nil) # => "1000%"
176-
# number_to_percentage('98a') # => "98a%"
177-
# number_to_percentage(100, format: '%n %') # => "100.000 %"
178-
# number_to_percentage(302.24398923423, precision: 5, round_mode: :down) # => "302.24398%"
179201
def number_to_percentage(number, options = {})
180202
NumberToPercentageConverter.convert(number, options)
181203
end

0 commit comments

Comments
 (0)