Method: LinuxStat::PrettifyBytes.convert_binary
- Defined in:
- lib/linux_stat/prettify_bytes.rb
.convert_binary(n, precision: 2) ⇒ 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”
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/linux_stat/prettify_bytes.rb', line 92 def convert_binary(n, precision: 2) if n < KIBI "#{"%.#{precision}f" % n} byte#{?s.freeze if n != 1}" elsif n < MEBI n /= KIBI "#{"%.#{precision}f" % n} kibibyte#{?s.freeze if n != 1}" elsif n < GIBI n /= MEBI "#{"%.#{precision}f" % n} mebibyte#{?s.freeze if n != 1}" elsif n < TEBI n /= GIBI "#{"%.#{precision}f" % n} gibibyte#{?s.freeze if n != 1}" elsif n < PEBI n /= TEBI "#{"%.#{precision}f" % n} tebibyte#{?s.freeze if n != 1}" elsif n < EXBI n /= PEBI "#{"%.#{precision}f" % n} pebibyte#{?s.freeze if n != 1}" elsif n < ZEBI n /= EXBI "#{"%.#{precision}f" % n} exbiyte#{?s.freeze if n != 1}" elsif n < YOBI n /= ZEBI "#{"%.#{precision}f" % n} zebibyte#{?s.freeze if n != 1}" else n /= YOBI "#{"%.#{precision}f" % n} yobibyte#{?s.freeze if n != 1}" end end |