Skip to content
This repository
Browse code

Fix number_to_human_size when using different precisions. Closes #7536.…

… [RichardStrand, mpalmer]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7275 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit ab5e1f15a67b22410835a84ba11a926f50b31cbe 1 parent c4c6662
Michael Koziarski authored August 05, 2007
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fix number_to_human_size when using different precisions. Closes #7536. [RichardStrand, mpalmer]
  4
+
3 5
 * Added partial layouts (see example in action_view/lib/partials.rb) [DHH]
4 6
 
5 7
 * Allow you to set custom :conditions on resource routes.  [Rick]
4  actionpack/lib/action_view/helpers/number_helper.rb
@@ -164,13 +164,13 @@ def number_with_precision(number, precision=3)
164 164
       def number_to_human_size(size, precision=1)
165 165
         size = Kernel.Float(size)
166 166
         case
167  
-          when size == 1        : "1 Byte"
  167
+          when size.to_i == 1   : "1 Byte"
168 168
           when size < 1.kilobyte: "%d Bytes" % size
169 169
           when size < 1.megabyte: "%.#{precision}f KB"  % (size / 1.0.kilobyte)
170 170
           when size < 1.gigabyte: "%.#{precision}f MB"  % (size / 1.0.megabyte)
171 171
           when size < 1.terabyte: "%.#{precision}f GB"  % (size / 1.0.gigabyte)
172 172
           else                    "%.#{precision}f TB"  % (size / 1.0.terabyte)
173  
-        end.sub('.0', '')
  173
+        end.sub(/([0-9])\.?0+ /, '\1 ' )
174 174
       rescue
175 175
         nil
176 176
       end
4  actionpack/test/template/number_helper_test.rb
@@ -83,6 +83,10 @@ def test_number_to_human_size
83 83
     assert_equal '1.18 MB',   number_to_human_size(1234567, 2)
84 84
     assert_equal '3 Bytes',   number_to_human_size(3.14159265, 4)
85 85
     assert_equal("123 Bytes", number_to_human_size("123"))
  86
+    assert_equal '1.01 KB',   number_to_human_size(1.0123.kilobytes, 2)
  87
+    assert_equal '1.01 KB',   number_to_human_size(1.0100.kilobytes, 4)
  88
+    assert_equal '10 KB',   number_to_human_size(10.000.kilobytes, 4)
  89
+    assert_equal '1 Byte',   number_to_human_size(1.1)
86 90
     assert_nil number_to_human_size('x')
87 91
     assert_nil number_to_human_size(nil)
88 92
   end

0 notes on commit ab5e1f1

Please sign in to comment.
Something went wrong with that request. Please try again.