+
+ orders = {
+ 1 => "bytes",
+ 1024 => "KiB",
+ (1024*1024) => "MiB",
+ (1024*1024*1024) => "GiB",
+ (1024*1024*1024*1024) => "TiB"
+ }
+
+ orders.each do |k, v|
+ sig = (n.to_f/k)
+ if sig >=1 and sig < 1024
+ if v == 'bytes'
+ return "%i #{v}" % sig
+ else
+ return "%0.1f #{v}" % sig
+ end
+ end