NumberToRoundedConverter should handle -Float::INFINITY

cc https://github.com/rails/rails/pull/42316#issuecomment-851606043
This commit is contained in:
Zachary Scott 2021-06-01 16:08:21 +09:00
parent 8a7a9d0a0b
commit bfdb634034
2 changed files with 5 additions and 5 deletions

@ -20,11 +20,7 @@ def convert
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 @@ def convert
a << b[0, precision]
end
a
else
# Infinity/NaN
"%f" % rounded_number
end
else
formatted_string = rounded_number

@ -208,6 +208,7 @@ def test_to_rounded_with_significant_digits
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