From bfdb634034f97fd30db579657c053c18cffb12c6 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Tue, 1 Jun 2021 16:08:21 +0900 Subject: [PATCH] NumberToRoundedConverter should handle -Float::INFINITY cc https://github.com/rails/rails/pull/42316#issuecomment-851606043 --- .../number_helper/number_to_rounded_converter.rb | 9 ++++----- activesupport/test/number_helper_test.rb | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb index 6bd1e1bb66..f48a5158c3 100644 --- a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb @@ -20,11 +20,7 @@ module ActiveSupport end formatted_string = - if rounded_number.nan? - "NaN" - elsif rounded_number.infinite? - "Inf" - else + if rounded_number.finite? s = rounded_number.to_s("F") a, b = s.split(".", 2) if precision != 0 @@ -33,6 +29,9 @@ module ActiveSupport a << b[0, precision] end a + else + # Infinity/NaN + "%f" % rounded_number end else formatted_string = rounded_number diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index ca9a02f536..9bdec994b7 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -208,6 +208,7 @@ module ActiveSupport assert_equal "9775." + "0" * 96, number_helper.number_to_rounded("9775", precision: 100, significant: true) assert_equal("97.7", number_helper.number_to_rounded(Rational(9772, 100), precision: 3, significant: true)) assert_equal "28729870200000000000000", number_helper.number_to_rounded(0.287298702e23.to_d, precision: 0, significant: true) + assert_equal "-Inf", number_helper.number_to_rounded(-Float::INFINITY, precision: 0, significant: true) end end