Module: Falqon::Pro::Web::Helpers::Sizes

Defined in:
lib/falqon/pro/web/helpers/sizes.rb

Constant Summary collapse

UNITS =
["B", "KB", "MB", "GB", "TB"].freeze

Instance Method Summary collapse

Instance Method Details

#size_in_words(bytes) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/falqon/pro/web/helpers/sizes.rb', line 10

def size_in_words(bytes)
  unit, exponent = UNITS.each_with_index.find { |_, i| (1024**(i + 1)) > bytes }

  bytes = bytes.to_f / (1024**exponent)
  bytes = bytes.to_i if bytes == bytes.to_i

  "#{bytes.round(2)} #{unit}"
end