Skip to content

Commit

Permalink
Merge pull request #42374 from rails/backport-42316-42341
Browse files Browse the repository at this point in the history
Backport #42316 and #42341 to 6-1-stable
  • Loading branch information
pixeltrix committed Jun 3, 2021
2 parents d83a318 + 17a4a0c commit 519b551
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,10 @@
* Fix bug in `number_with_precision` when using large `BigDecimal` values.

Fixes #42302.

*Federico Aldunate*, *Zachary Scott*


## Rails 6.1.3.2 (May 05, 2021) ##

* No changes.
Expand Down
Expand Up @@ -20,14 +20,18 @@ def convert
end

formatted_string =
if rounded_number.nan? || rounded_number.infinite? || rounded_number == rounded_number.to_i
"%00.#{precision}f" % rounded_number
else
if rounded_number.finite?
s = rounded_number.to_s("F")
s << "0" * precision
a, b = s.split(".", 2)
a << "."
a << b[0, precision]
if precision != 0
b << "0" * precision
a << "."
a << b[0, precision]
end
a
else
# Infinity/NaN
"%f" % rounded_number
end
else
formatted_string = rounded_number
Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -207,6 +207,8 @@ def test_to_rounded_with_significant_digits
assert_equal "9775.0000000000000000", number_helper.number_to_rounded("9775", precision: 20, significant: true)
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 519b551

Please sign in to comment.