`number_to_percentage` and `precision: 0` work with `NAN` and `INFINITY`.

Closes #19227.
This commit is contained in:
Yves Senn 2015-03-06 09:04:51 +01:00
parent 1c1bced7b1
commit 081a3963ea
4 changed files with 16 additions and 1 deletions

View File

@ -35,6 +35,10 @@ class NumberHelperTest < ActionView::TestCase
assert_equal "98a%", number_to_percentage("98a")
assert_equal "NaN%", number_to_percentage(Float::NAN)
assert_equal "Inf%", number_to_percentage(Float::INFINITY)
assert_equal "NaN%", number_to_percentage(Float::NAN, precision: 0)
assert_equal "Inf%", number_to_percentage(Float::INFINITY, precision: 0)
assert_equal "NaN%", number_to_percentage(Float::NAN, precision: 1)
assert_equal "Inf%", number_to_percentage(Float::INFINITY, precision: 1)
end
def test_number_with_delimiter

View File

@ -1,3 +1,10 @@
* `number_to_percentage` does not crash with `Float::NAN` or `Float::INFINITY`
as input when `precision: 0` is used.
Fixes #19227.
*Yves Senn*
* Take DST into account when locating TimeZone from Numeric.
When given a specific offset, use the first result found where the

View File

@ -23,7 +23,7 @@ module ActiveSupport
precision = 0 if precision < 0 # don't let it be negative
else
rounded_number = number.round(precision)
rounded_number = rounded_number.to_i if precision == 0
rounded_number = rounded_number.to_i if precision == 0 && rounded_number.finite?
rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros
end

View File

@ -83,6 +83,10 @@ module ActiveSupport
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("NaN%", number_helper.number_to_percentage(Float::NAN, precision: 0))
assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY, precision: 0))
assert_equal("NaN%", number_helper.number_to_percentage(Float::NAN, precision: 1))
assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY, precision: 1))
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))