diff options
author | Juanjo Bazán <jjbazan@gmail.com> | 2014-05-28 12:40:42 +0200 |
---|---|---|
committer | Juanjo Bazán <jjbazan@gmail.com> | 2014-05-31 20:07:47 +0200 |
commit | 60062cf39a6079cf6a47a754f4b5fc6c474b9b15 (patch) | |
tree | 80d7dfb54a9b71c2101442716e27897f8a5b4b4f | |
parent | 4d83c2a27fb5bea5ccba468aaad4a9038a83b2b8 (diff) | |
download | rails-60062cf39a6079cf6a47a754f4b5fc6c474b9b15.tar.gz rails-60062cf39a6079cf6a47a754f4b5fc6c474b9b15.tar.bz2 rails-60062cf39a6079cf6a47a754f4b5fc6c474b9b15.zip |
Fix AS::NumberHelper results with rationals
:precision was incorrectly being applied to Rationals
before:
ActiveSupport::NumberHelper.number_to_rounded Rational(10, 3), precision: 2
=> "3.3"
after:
ActiveSupport::NumberHelper.number_to_rounded Rational(10, 3), precision: 2
=> "3.33"
-rw-r--r-- | actionview/test/template/number_helper_test.rb | 1 | ||||
-rw-r--r-- | activesupport/CHANGELOG.md | 13 | ||||
-rw-r--r-- | activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb | 6 | ||||
-rw-r--r-- | activesupport/test/number_helper_test.rb | 2 |
4 files changed, 17 insertions, 5 deletions
diff --git a/actionview/test/template/number_helper_test.rb b/actionview/test/template/number_helper_test.rb index adb888319d..0495224d04 100644 --- a/actionview/test/template/number_helper_test.rb +++ b/actionview/test/template/number_helper_test.rb @@ -48,6 +48,7 @@ class NumberHelperTest < ActionView::TestCase assert_equal "-111.235", number_with_precision(-111.2346) assert_equal "111.00", number_with_precision(111, precision: 2) assert_equal "0.00100", number_with_precision(0.001, precision: 5) + assert_equal "3.33", number_with_precision(Rational(10, 3), precision: 2) end def test_number_to_human_size diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 38ec3c0753..8a9d31a7bb 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,16 @@ +* Fixed precision error in NumberHelper when using Rationals. + + before: + ActiveSupport::NumberHelper.number_to_rounded Rational(1000, 3), precision: 2 + #=> "330.00" + after: + ActiveSupport::NumberHelper.number_to_rounded Rational(1000, 3), precision: 2 + #=> "333.33" + + See #15379. + + *Juanjo Bazán* + * Removed deprecated `Numeric#ago` and friends Replacements: 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 c45f6cdcfa..01597b288a 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 @@ -12,11 +12,7 @@ module ActiveSupport when Float, String @number = BigDecimal(number.to_s) when Rational - if significant - @number = BigDecimal(number, digit_count(number.to_i) + precision) - else - @number = BigDecimal(number, precision) - end + @number = BigDecimal(number, digit_count(number.to_i) + precision) else @number = number.to_d end diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index a7a0ae02e7..bb51cc68f2 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -134,6 +134,7 @@ module ActiveSupport assert_equal("111.23460000000000000000", number_helper.number_to_rounded('111.2346', :precision => 20)) 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)) end end @@ -174,6 +175,7 @@ module ActiveSupport assert_equal "9775.0000000000000000", number_helper.number_to_rounded(BigDecimal(9775), :precision => 20, :significant => true ) 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)) end end |