From 597a9276671f5d7884a0ee1a617656dd5ee4b0ea Mon Sep 17 00:00:00 2001 From: Max Jacobson Date: Tue, 14 Jul 2015 00:54:56 -0400 Subject: [PATCH 1/2] Round some numbers more humanely Fix #20869 --- activesupport/CHANGELOG.md | 5 +++++ .../number_helper/number_to_human_converter.rb | 6 ++++-- activesupport/test/number_helper_test.rb | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 7148f289bb..38c825bc05 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix `number_to_human` rounding 999999999 to "1000 Million" instead of + "1 Billion". + + *Max Jacobson* + * Fix `TimeWithZone#eql?` to properly handle `TimeWithZone` created from `DateTime`: twz = DateTime.now.in_time_zone twz.eql?(twz.dup) => true 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 @@ def convert # :nodoc: 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 83efbffdfb..3610d39daa 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -293,6 +293,8 @@ def test_number_to_human 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 From 7eb7d6f7c1bda62c160420f0c3b99442a7967c1f Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 20 Oct 2015 15:38:43 -0600 Subject: [PATCH 2/2] Update the changelog for #20872 to be a bit less confusing --- activesupport/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 38c825bc05..4ad45b4a99 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,5 @@ -* Fix `number_to_human` rounding 999999999 to "1000 Million" instead of - "1 Billion". +* Fix `number_to_human` so that 999999999 rounds to "1 Billion" instead of + "1000 Million". *Max Jacobson*