Class: MF60::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/mf60/helpers.rb

Overview

format bytes as 12B, 124K, 1.2M, 24G etc

Class Method Summary collapse

Class Method Details

.bytes_formatted(bytes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mf60/helpers.rb', line 4

def self.bytes_formatted(bytes)
  return '-' if bytes <= 0
  units = ['B','K','M','G','T']
  t = 0
  while bytes >= 1024**(t+1)
    t+=1
  end
  n = t>0 ? (bytes/(1024.0**t)) : bytes
  n = n.floor if (n-n.floor)<0.1
  n = n.ceil if (n.ceil-n)<0.1
  if n==1024
    n = n/1024
    t+=1
  end
  fn = (n-n.floor>0 and n<50) ? sprintf("%.1f", n) : n.ceil
  "#{fn}#{units[t]}"
end