From 0eae62525696b57fe7fc4bbb0bf9c0bc7ee4e480 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Wed, 16 Mar 2011 19:03:25 +0100 Subject: [PATCH] fixes an issue with number_to_human when converting values which are less than 1 but greater than -1 [#6576 state:resolved] Signed-off-by: Santiago Pastorino --- actionpack/lib/action_view/helpers/number_helper.rb | 2 +- actionpack/test/template/number_helper_test.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 4e44843c4b..05a9c5b4f1 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -472,7 +472,7 @@ module ActionView end.keys.map{|e_name| inverted_du[e_name] }.sort_by{|e| -e} number_exponent = number != 0 ? Math.log10(number.abs).floor : 0 - display_exponent = unit_exponents.find{|e| number_exponent >= e } + display_exponent = unit_exponents.find{ |e| number_exponent >= e } || 0 number /= 10 ** display_exponent unit = case units diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index c1b4bab903..c8d50ebf75 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -195,7 +195,9 @@ class NumberHelperTest < ActionView::TestCase def test_number_to_human assert_equal '-123', number_to_human(-123) - assert_equal '0', number_to_human(0) + assert_equal '-0.5', number_to_human(-0.5) + assert_equal '0', number_to_human(0) + assert_equal '0.5', number_to_human(0.5) assert_equal '123', number_to_human(123) assert_equal '1.23 Thousand', number_to_human(1234) assert_equal '12.3 Thousand', number_to_human(12345)