Merge pull request #20872 from maxjacobson/more-humane-rounding

Round some numbers more humanely
This commit is contained in:
Sean Griffin 2015-10-20 15:39:31 -06:00
commit e8c29853ff
3 changed files with 11 additions and 2 deletions

@ -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.

@ -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

@ -296,6 +296,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