From 363b79f9428395249d7509ddc88e1080902d6256 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Thu, 19 Jan 2006 05:31:48 +0000 Subject: Refactor human_size to exclude decimal place if it is zero. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3437 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/number_helper.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index e06eb14f7f..9098dd8c33 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -85,15 +85,15 @@ module ActionView # human_size(1234567) => 1.2 MB # human_size(1234567890) => 1.1 GB def number_to_human_size(size) - begin - return "%d Bytes" % size if size < 1.kilobytes - return "%.1f KB" % (size/1.0.kilobytes) if size < 1.megabytes - return "%.1f MB" % (size/1.0.megabytes) if size < 1.gigabytes - return "%.1f GB" % (size/1.0.gigabytes) if size < 1.terabytes - return "%.1f TB" % (size/1.0.terabytes) - rescue - # just return nothing - end + case + when size < 1.kilobyte: '%d Bytes' % size + when size < 1.megabyte: '%.1f KB' % (size / 1.0.kilobyte) + when size < 1.gigabyte: '%.1f MB' % (size / 1.0.megabyte) + when size < 1.terabyte: '%.1f GB' % (size / 1.0.gigabyte) + else '%.1f TB' % (size / 1.0.terabyte) + end.sub('.0', '') + rescue + nil end alias_method :human_size, :number_to_human_size # deprecated alias -- cgit v1.2.3