Module: Fanta::NumberHelper

Defined in:
lib/fanta/helpers/number_helper.rb

Instance Method Summary collapse

Instance Method Details

#bytes_to_human_size(bytes, precision = 1) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fanta/helpers/number_helper.rb', line 7

def bytes_to_human_size bytes, precision = 1
  kb = 1024
  mb = 1024 * kb
  gb = 1024 * mb
  case 
    when bytes < kb; "%d #{plural(bytes, 'byte', 'bytes')}" % bytes
    when bytes < mb; "%.#{precision}f #{plural((bytes / kb), 'kilobyte', 'kilobytes')}" % (bytes / kb)
    when bytes < gb; "%.#{precision}f #{plural((bytes / mb), 'megabyte', 'megabytes')}" % (bytes / mb)
    when bytes >= gb; "%.#{precision}f #{plural((bytes / gb), 'gigabyte', 'gigabytes')}" % (bytes / gb)
  end
end

#plural(count, singular, plural) ⇒ Object



3
4
5
# File 'lib/fanta/helpers/number_helper.rb', line 3

def plural count, singular, plural
  count == 1 ? singular : plural
end