Skip to content

Commit

Permalink
Ensure number_to_human_size does not strip zeros from the end [#1763
Browse files Browse the repository at this point in the history
…state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
audiodude authored and NZKoz committed Oct 17, 2009
1 parent 459749c commit eb30c69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion actionpack/lib/action_view/helpers/number_helper.rb
Expand Up @@ -246,6 +246,11 @@ def number_with_precision(number, *args)
# number_to_human_size(483989, :precision => 0) # => 473 KB
# number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
#
# Zeros after the decimal point are always stripped out, regardless of the
# specified precision:
# helper.number_to_human_size(1234567890123, :precision => 5) # => "1.12283 TB"
# helper.number_to_human_size(524288000, :precision=>5) # => "500 MB"
#
# You can still use <tt>number_to_human_size</tt> with the old API that accepts the
# +precision+ as its optional second parameter:
# number_to_human_size(1234567, 2) # => 1.18 MB
Expand Down Expand Up @@ -291,7 +296,7 @@ def number_to_human_size(number, *args)
:precision => precision,
:separator => separator,
:delimiter => delimiter
).sub(/(\d)(#{escaped_separator}[1-9]*)?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
rescue
number
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/template/number_helper_test.rb
Expand Up @@ -118,6 +118,10 @@ def test_number_to_human_size_with_options_hash
assert_equal '1.01 KB', number_to_human_size(1.0123.kilobytes, :precision => 2)
assert_equal '1.01 KB', number_to_human_size(1.0100.kilobytes, :precision => 4)
assert_equal '10 KB', number_to_human_size(10.000.kilobytes, :precision => 4)
assert_equal '1 TB', number_to_human_size(1234567890123, :precision => 0)
assert_equal '500 MB', number_to_human_size(524288000, :precision=>0)
assert_equal '40 KB', number_to_human_size(41010, :precision => 0)
assert_equal '40 KB', number_to_human_size(41100, :precision => 0)
end

def test_number_to_human_size_with_custom_delimiter_and_separator
Expand Down

0 comments on commit eb30c69

Please sign in to comment.