Skip to content

Commit

Permalink
NumberToRoundedConverter should handle -Float::INFINITY
Browse files Browse the repository at this point in the history
  • Loading branch information
zzak committed Jun 1, 2021
1 parent 8a7a9d0 commit bfdb634
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Expand Up @@ -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
Expand All @@ -33,6 +29,9 @@ def convert
a << b[0, precision]
end
a
else
# Infinity/NaN
"%f" % rounded_number
end
else
formatted_string = rounded_number
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -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

Expand Down

0 comments on commit bfdb634

Please sign in to comment.