Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix `number_to_percentage` with `Float::NAN`, `Float::INFINITY`.

Closes #14405.

This is a follow-up to 9e997e9 to restore
the documented behavior.

Conflicts:
	actionview/CHANGELOG.md
  • Loading branch information...
commit 2be3a2c698941b15265937e349b2f9eac73a7711 1 parent a76a8d4
@senny senny authored
View
7 actionview/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `number_to_percentage` does not crash with `Float::NAN` or `Float::INFINITY`
+ as input.
+
+ Fixes #14405.
+
+ *Yves Senn*
+
* Take variants into account when calculating template digests in ActionView::Digestor.
The arguments to ActionView::Digestor#digest are now being passed as a hash
View
3  actionview/test/template/number_helper_test.rb
@@ -32,6 +32,9 @@ def test_number_to_percentage
assert_equal "100%", number_to_percentage(100, precision: 0)
assert_equal "123.4%", number_to_percentage(123.400, precision: 3, strip_insignificant_zeros: true)
assert_equal "1.000,000%", number_to_percentage(1000, delimiter: ".", separator: ",")
+ assert_equal "98a%", number_to_percentage("98a")
+ assert_equal "NaN%", number_to_percentage(Float::NAN)
+ assert_equal "Inf%", number_to_percentage(Float::INFINITY)
end
def test_number_with_delimiter
View
3  activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb
@@ -32,8 +32,7 @@ def convert
end
formatted_string =
- case rounded_number
- when BigDecimal
+ if BigDecimal === rounded_number && rounded_number.finite?
s = rounded_number.to_s('F') + '0'*precision
a, b = s.split('.', 2)
a + '.' + b[0, precision]
View
3  activesupport/test/number_helper_test.rb
@@ -79,6 +79,9 @@ def test_number_to_percentage
assert_equal("123.4%", number_helper.number_to_percentage(123.400, :precision => 3, :strip_insignificant_zeros => true))
assert_equal("1.000,000%", number_helper.number_to_percentage(1000, :delimiter => '.', :separator => ','))
assert_equal("1000.000 %", number_helper.number_to_percentage(1000, :format => "%n %"))
+ 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))
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.