Sha256: b2220980f1729831d2f2fb3b13b34bc06042671764992fedac02989ec07d7824

Contents?: true

Size: 750 Bytes

Versions: 1

Compression:

Stored size: 750 Bytes

Contents

module NiceBytes
	K = 2.0**10
	M = 2.0**20
	G = 2.0**30
	T = 2.0**40
	def nice_bytes( bytes, max_digits=3 )
		value, suffix, precision = case bytes
			when 0...K
				[ bytes, 'B', 0 ]
			else
				value, suffix = case bytes
					when K...M then [ bytes / K, 'kB' ]
					when M...G then [ bytes / M, 'MB' ]
					when G...T then [ bytes / G, 'GB' ]
					else            [ bytes / T, 'TB' ]
				end
				used_digits = case value
					when   0...10   then 1
					when  10...100  then 2
					when 100...1000 then 3
					else 4
				end
				leftover_digits = max_digits - used_digits
				[ value, suffix, leftover_digits > 0 ? leftover_digits : 0 ]
		end
		"%.#{precision}f#{suffix}" % value
	end
	module_function :nice_bytes
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
RUIC-0.6.1 lib/ruic/nicebytes.rb