提交 d7318599 编写于 作者: R Rafael Mendonça França

Merge branch 'rm-ntp'

Merge #12067 rebasing and improving the code.
* Enable number_to_percentage to keep the number's precision by allowing :precision to be nil
*Jack Xu*
* config_accessor became a private method, as with Ruby's attr_accessor.
*Akira Matsuda*
......
......@@ -94,7 +94,7 @@ def number_to_currency(number, options = {})
# * <tt>:locale</tt> - Sets the locale to be used for formatting
# (defaults to current locale).
# * <tt>:precision</tt> - Sets the precision of the number
# (defaults to 3).
# (defaults to 3). Keeps the number's precision if nil.
# * <tt>:significant</tt> - If +true+, precision will be the #
# of significant_digits. If +false+, the # of fractional
# digits (defaults to +false+).
......@@ -116,6 +116,7 @@ def number_to_currency(number, options = {})
# number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
# number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
# number_to_percentage(1000, locale: :fr) # => 1 000,000%
# number_to_percentage:(1000, precision: nil) # => 1000%
# number_to_percentage('98a') # => 98a%
# number_to_percentage(100, format: '%n %') # => 100 %
def number_to_percentage(number, options = {})
......@@ -161,7 +162,7 @@ def number_to_delimited(number, options = {})
# * <tt>:locale</tt> - Sets the locale to be used for formatting
# (defaults to current locale).
# * <tt>:precision</tt> - Sets the precision of the number
# (defaults to 3).
# (defaults to 3). Keeps the number's precision if nil.
# * <tt>:significant</tt> - If +true+, precision will be the #
# of significant_digits. If +false+, the # of fractional
# digits (defaults to +false+).
......@@ -182,6 +183,7 @@ def number_to_delimited(number, options = {})
# number_to_rounded(111.2345, significant: true) # => 111
# number_to_rounded(111.2345, precision: 1, significant: true) # => 100
# number_to_rounded(13, precision: 5, significant: true) # => 13.000
# number_to_rounded(13, precision: nil) # => 13
# number_to_rounded(111.234, locale: :fr) # => 111,234
#
# number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
......
......@@ -6,8 +6,8 @@ class NumberToRoundedConverter < NumberConverter # :nodoc:
def convert
precision = options.delete :precision
significant = options.delete :significant
if precision
case number
when Float, String
@number = BigDecimal(number.to_s)
......@@ -17,7 +17,7 @@ def convert
@number = number.to_d
end
if significant && precision > 0
if options.delete(:significant) && precision > 0
digits, rounded_number = digits_and_rounded_number(precision)
precision -= digits
precision = 0 if precision < 0 # don't let it be negative
......@@ -35,6 +35,9 @@ def convert
else
"%00.#{precision}f" % rounded_number
end
else
formatted_string = number
end
delimited_number = NumberToDelimitedConverter.convert(formatted_string, options)
format_number(delimited_number)
......
......@@ -83,6 +83,10 @@ def test_number_to_percentage
assert_equal("98a%", number_helper.number_to_percentage("98a"))
assert_equal("NaN%", number_helper.number_to_percentage(Float::NAN))
assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY))
assert_equal("1000%", number_helper.number_to_percentage(1000, precision: nil))
assert_equal("1000%", number_helper.number_to_percentage(1000, precision: nil))
assert_equal("1000.1%", number_helper.number_to_percentage(1000.1, precision: nil))
assert_equal("-0.13 %", number_helper.number_to_percentage("-0.13", precision: nil, format: "%n %"))
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册