aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2014-08-20 16:10:37 +0200
committerXavier Noria <fxn@hashref.com>2014-08-20 16:25:35 +0200
commit500deece9ef511c59a8c6edc7fb8682b13c4cd7e (patch)
tree38baa5e3fa8014fde4b9eea4478ce39cac8d576d
parent3a1edcff09fb7ff9c7246d71525f4c23c61e113d (diff)
downloadrails-500deece9ef511c59a8c6edc7fb8682b13c4cd7e.tar.gz
rails-500deece9ef511c59a8c6edc7fb8682b13c4cd7e.tar.bz2
rails-500deece9ef511c59a8c6edc7fb8682b13c4cd7e.zip
Fixes the digits counter of AS's NumberToRoundedConverter
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.
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb2
-rw-r--r--activesupport/test/number_helper_test.rb1
2 files changed, 2 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb
index 01597b288a..47028e9ecf 100644
--- a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb
+++ b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb
@@ -59,7 +59,7 @@ module ActiveSupport
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
diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb
index bb51cc68f2..50d84a9470 100644
--- a/activesupport/test/number_helper_test.rb
+++ b/activesupport/test/number_helper_test.rb
@@ -135,6 +135,7 @@ module ActiveSupport
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