Sha256: dd189a3fbd8ea6e714e09a00596b6841e19db4bdae79a473e727a60e6496361a

Contents?: true

Size: 743 Bytes

Versions: 25

Compression:

Stored size: 743 Bytes

Contents

# encoding: utf-8
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, 'kiB' ]
					when M...G then [ bytes / M, 'MiB' ]
					when G...T then [ bytes / G, 'GiB' ]
					else            [ bytes / T, 'TiB' ]
				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

25 entries across 25 versions & 1 rubygems

Version Path
gaptool-server-0.4.13 lib/helpers/nicebytes.rb
gaptool-server-0.4.12 lib/helpers/nicebytes.rb
gaptool-server-0.4.11 lib/helpers/nicebytes.rb
gaptool-server-0.4.10 lib/helpers/nicebytes.rb
gaptool-server-0.4.9 lib/helpers/nicebytes.rb
gaptool-server-0.4.8 lib/helpers/nicebytes.rb
gaptool-server-0.4.7 lib/helpers/nicebytes.rb
gaptool-server-0.4.6 lib/helpers/nicebytes.rb
gaptool-server-0.4.5 lib/helpers/nicebytes.rb
gaptool-server-0.4.4 lib/helpers/nicebytes.rb
gaptool-server-0.4.3 lib/helpers/nicebytes.rb
gaptool-server-0.4.2 lib/helpers/nicebytes.rb
gaptool-server-0.4.1 lib/helpers/nicebytes.rb
gaptool-server-0.4.0 lib/helpers/nicebytes.rb
gaptool-server-0.3.8 lib/helpers/nicebytes.rb
gaptool-server-0.3.7 lib/helpers/nicebytes.rb
gaptool-server-0.3.6 lib/helpers/nicebytes.rb
gaptool-server-0.3.3 lib/helpers/nicebytes.rb
gaptool-server-0.3.2 lib/helpers/nicebytes.rb
gaptool-server-0.3.1 lib/helpers/nicebytes.rb