Skip to content

Commit

Permalink
Fixes the digits counter of AS's NumberToRoundedConverter
Browse files Browse the repository at this point in the history
Zero has one digit, but Math.log10(0) returns -Infinity. The method
needs to special-case zero.

The patch adds a regression test that is not clearly related to the
underlying issue because digit_count is private and has no coverage.
Gray area.

This bug was uncovered by 60062cf.
  • Loading branch information
fxn committed Aug 20, 2014
1 parent 3a1edcf commit 500deec
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
Expand Up @@ -59,7 +59,7 @@ def calculate_rounded_number(multiplier)
end

def digit_count(number)
(Math.log10(absolute_number(number)) + 1).floor
number.zero? ? 1 : (Math.log10(absolute_number(number)) + 1).floor
end

def strip_insignificant_zeros
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -135,6 +135,7 @@ def test_to_rounded
assert_equal("111.23460000000000000000", number_helper.number_to_rounded(BigDecimal(111.2346, Float::DIG), :precision => 20))
assert_equal("111.2346" + "0"*96, number_helper.number_to_rounded('111.2346', :precision => 100))
assert_equal("111.2346", number_helper.number_to_rounded(Rational(1112346, 10000), :precision => 4))
assert_equal('0.00', number_helper.number_to_rounded(Rational(0, 1), :precision => 2))
end
end

Expand Down

0 comments on commit 500deec

Please sign in to comment.