Skip to content

Commit

Permalink
number_to_percentage and precision: 0 work with NAN and `INFINI…
Browse files Browse the repository at this point in the history
…TY`.

Closes #19227.

Conflicts:
	activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb
	activesupport/test/number_helper_test.rb
  • Loading branch information
senny committed Mar 6, 2015
1 parent 1c73eb6 commit 51d548a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actionview/test/template/number_helper_test.rb
Expand Up @@ -35,6 +35,10 @@ def test_number_to_percentage
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
Expand Down
7 changes: 7 additions & 0 deletions activesupport/CHANGELOG.md
@@ -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
Expand Down
Expand Up @@ -23,7 +23,7 @@ def convert
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

Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -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("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))
end
end

Expand Down

0 comments on commit 51d548a

Please sign in to comment.