Method: LinuxStat::PrettifyBytes.convert_binary

Defined in:
lib/linux_stat/prettify_bytes.rb

.convert_binary(n) ⇒ Object

Converts a number to binary byte units and outputs with the IEC prefix For example,

LinuxStat::PrettifyBytes.convert_binary(1000)

> “1000.0 bytes”

LinuxStat::PrettifyBytes.convert_binary(1000 ** 3)

> “953.67 mebibytes”

LinuxStat::PrettifyBytes.convert_binary(1024 ** 3)

> “1.0 gibibyte”



42
43
44
45
46
47
48
49
# File 'lib/linux_stat/prettify_bytes.rb', line 42

def convert_binary(n)
	@@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
		.map.with_index { |x, i| [x, 1024.**(i + 1)] }
	unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]

	converted = n.fdiv(unit[1] / 1024).round(2)
	"#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
end