aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-10-20 15:39:31 -0600
committerSean Griffin <sean@seantheprogrammer.com>2015-10-20 15:39:31 -0600
commite8c29853ff0916ae8174dae522bb8bb487dfd142 (patch)
tree80f309cdead2acfc1f312212cf32335a6bef4ad9 /activesupport
parente200fe97f5b2ce62e2be1718c449293d07d77584 (diff)
parent7eb7d6f7c1bda62c160420f0c3b99442a7967c1f (diff)
downloadrails-e8c29853ff0916ae8174dae522bb8bb487dfd142.tar.gz
rails-e8c29853ff0916ae8174dae522bb8bb487dfd142.tar.bz2
rails-e8c29853ff0916ae8174dae522bb8bb487dfd142.zip
Merge pull request #20872 from maxjacobson/more-humane-rounding
Round some numbers more humanely
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md5
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_human_converter.rb6
-rw-r--r--activesupport/test/number_helper_test.rb2
3 files changed, 11 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 92efd96de4..c02c5a82f5 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Fix `number_to_human` so that 999999999 rounds to "1 Billion" instead of
+ "1000 Million".
+
+ *Max Jacobson*
+
* Fix `ActiveSupport::Deprecation#deprecate_methods` to report using the
current deprecator instance, where applicable.
diff --git a/activesupport/lib/active_support/number_helper/number_to_human_converter.rb b/activesupport/lib/active_support/number_helper/number_to_human_converter.rb
index 5c6fe2df83..7a1f8171c0 100644
--- a/activesupport/lib/active_support/number_helper/number_to_human_converter.rb
+++ b/activesupport/lib/active_support/number_helper/number_to_human_converter.rb
@@ -20,9 +20,11 @@ module ActiveSupport
exponent = calculate_exponent(units)
@number = number / (10 ** exponent)
+ until (rounded_number = NumberToRoundedConverter.convert(number, options)) != NumberToRoundedConverter.convert(1000, options)
+ @number = number / 1000.0
+ exponent += 3
+ end
unit = determine_unit(units, exponent)
-
- rounded_number = NumberToRoundedConverter.convert(number, options)
format.gsub('%n'.freeze, rounded_number).gsub('%u'.freeze, unit).strip
end
diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb
index 944bce1b41..7f62d7c0b3 100644
--- a/activesupport/test/number_helper_test.rb
+++ b/activesupport/test/number_helper_test.rb
@@ -296,6 +296,8 @@ module ActiveSupport
assert_equal '1.2346 Million', number_helper.number_to_human(1234567, :precision => 4, :significant => false)
assert_equal '1,2 Million', number_helper.number_to_human(1234567, :precision => 1, :significant => false, :separator => ',')
assert_equal '1 Million', number_helper.number_to_human(1234567, :precision => 0, :significant => true, :separator => ',') #significant forced to false
+ assert_equal '1 Million', number_helper.number_to_human(999999)
+ assert_equal '1 Billion', number_helper.number_to_human(999999999)
end
end