Skip to content

Commit f5c9a7e

Browse files
Clean up AS::NumberHelper#number_to_rounded doc [ci-skip]
(cherry picked from commit 8a04207)
1 parent 6726ede commit f5c9a7e

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

activesupport/lib/active_support/number_helper.rb

+47-38
Original file line numberDiff line numberDiff line change
@@ -243,49 +243,58 @@ def number_to_delimited(number, options = {})
243243
NumberToDelimitedConverter.convert(number, options)
244244
end
245245

246-
# Formats a +number+ with the specified level of
247-
# <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
248-
# +:significant+ is +false+, and 5 if +:significant+ is +true+).
249-
# You can customize the format in the +options+ hash.
246+
# Formats +number+ to a specific level of precision.
247+
#
248+
# number_to_rounded(12345.6789) # => "12345.679"
249+
# number_to_rounded(12345.6789, precision: 2) # => "12345.68"
250+
# number_to_rounded(12345.6789, precision: 0) # => "12345"
251+
# number_to_rounded(12345, precision: 5) # => "12345.00000"
250252
#
251253
# ==== Options
252254
#
253-
# * <tt>:locale</tt> - Sets the locale to be used for formatting
254-
# (defaults to current locale).
255-
# * <tt>:precision</tt> - Sets the precision of the number
256-
# (defaults to 3). Keeps the number's precision if +nil+.
257-
# * <tt>:round_mode</tt> - Determine how rounding is performed
258-
# (defaults to :default. See BigDecimal::mode)
259-
# * <tt>:significant</tt> - If +true+, precision will be the number
260-
# of significant_digits. If +false+, the number of fractional
261-
# digits (defaults to +false+).
262-
# * <tt>:separator</tt> - Sets the separator between the
263-
# fractional and integer digits (defaults to ".").
264-
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
265-
# to "").
266-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
267-
# insignificant zeros after the decimal separator (defaults to
268-
# +false+).
255+
# [+:locale+]
256+
# The locale to use for formatting. Defaults to the current locale.
269257
#
270-
# ==== Examples
258+
# number_to_rounded(111.234, locale: :fr)
259+
# # => "111,234"
260+
#
261+
# [+:precision+]
262+
# The level of precision, or +nil+ to preserve +number+'s precision.
263+
# Defaults to 3.
264+
#
265+
# number_to_rounded(12345.6789, precision: nil)
266+
# # => "12345.6789"
267+
#
268+
# [+:round_mode+]
269+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
270+
# +:default+.
271+
#
272+
# number_to_rounded(12.34, precision: 0, round_mode: :up)
273+
# # => "13"
274+
#
275+
# [+:significant+]
276+
# Whether +:precision+ should be applied to significant digits instead of
277+
# fractional digits. Defaults to false.
278+
#
279+
# number_to_rounded(12345.6789) # => "12345.679"
280+
# number_to_rounded(12345.6789, significant: true) # => "12300"
281+
# number_to_rounded(12345.6789, precision: 2) # => "12345.68"
282+
# number_to_rounded(12345.6789, precision: 2, significant: true) # => "12000"
283+
#
284+
# [+:separator+]
285+
# The decimal separator. Defaults to <tt>"."</tt>.
286+
#
287+
# [+:delimiter+]
288+
# The thousands delimiter. Defaults to <tt>","</tt>.
289+
#
290+
# [+:strip_insignificant_zeros+]
291+
# Whether to remove insignificant zeros after the decimal separator.
292+
# Defaults to false.
293+
#
294+
# number_to_rounded(12.34, strip_insignificant_zeros: false) # => "12.340"
295+
# number_to_rounded(12.34, strip_insignificant_zeros: true) # => "12.34"
296+
# number_to_rounded(12.3456, strip_insignificant_zeros: true) # => "12.346"
271297
#
272-
# number_to_rounded(111.2345) # => "111.235"
273-
# number_to_rounded(111.2345, precision: 2) # => "111.23"
274-
# number_to_rounded(13, precision: 5) # => "13.00000"
275-
# number_to_rounded(389.32314, precision: 0) # => "389"
276-
# number_to_rounded(111.2345, significant: true) # => "111"
277-
# number_to_rounded(111.2345, precision: 1, significant: true) # => "100"
278-
# number_to_rounded(13, precision: 5, significant: true) # => "13.000"
279-
# number_to_rounded(13, precision: nil) # => "13"
280-
# number_to_rounded(389.32314, precision: 0, round_mode: :up) # => "390"
281-
# number_to_rounded(111.234, locale: :fr) # => "111,234"
282-
#
283-
# number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
284-
# # => "13"
285-
#
286-
# number_to_rounded(389.32314, precision: 4, significant: true) # => "389.3"
287-
# number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
288-
# # => "1.111,23"
289298
def number_to_rounded(number, options = {})
290299
NumberToRoundedConverter.convert(number, options)
291300
end

0 commit comments

Comments
 (0)