Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BigDecimal compatible operation in NumberToRoundedConverter #42316

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,14 +20,19 @@ def convert
end

formatted_string =
if rounded_number.nan? || rounded_number.infinite? || rounded_number == rounded_number.to_i
"%00.#{precision}f" % rounded_number
if rounded_number.nan?
"NaN"
elsif rounded_number.infinite?
"Inf"
else
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
end
else
formatted_string = rounded_number
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -207,6 +207,7 @@ 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)
end
end

Expand Down