Module: LinuxStat::PrettifyBytes
- Defined in:
- lib/linux_stat/prettify_bytes.rb
Overview
Helps you convert bytes to a unit like:
-
kilobyte, megabyte, gigabyte, terabyte, petabyte, exabyte, zettabyte, yottabyte
-
kibibyte, mebibyte, gibibyte, tebibyte, pebibyte, exbibyte, zebibyte, yobibyte
-
kB, MB, GB, TB, PB, EB, ZB, YB
-
kiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
Class Method Summary collapse
-
.convert_binary(n, precision: 2) ⇒ Object
Converts a number to binary byte units and outputs with the IEC prefix For example,.
-
.convert_decimal(n, precision: 2) ⇒ Object
Converts a number to decimal byte units and outputs with the metric prefix For example,.
-
.convert_short_binary(n, precision: 2) ⇒ Object
Converts a number to binary byte units.
-
.convert_short_decimal(n, precision: 2) ⇒ Object
Converts a number to decimal byte units For example,.
Class Method Details
.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”
49 50 51 52 53 54 55 56 |
# File 'lib/linux_stat/prettify_bytes.rb', line 49 def convert_binary(n, precision: 2) @@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, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}" end |
.convert_decimal(n, precision: 2) ⇒ Object
Converts a number to decimal byte units and outputs with the metric prefix For example,
LinuxStat::PrettifyBytes.convert_decimal(1000)
> “1.0 kilobyte”
LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3)
> “1.0 gigabyte”
LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3)
> “1.07 gigabytes”
26 27 28 29 30 31 32 33 |
# File 'lib/linux_stat/prettify_bytes.rb', line 26 def convert_decimal(n, precision: 2) @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta) .map.with_index { |x, i| [x, 1000.**(i + 1)] } unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27] converted = n.fdiv(unit[1] / 1000).round(2) "#{pad_left(converted, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}" end |
.convert_short_binary(n, precision: 2) ⇒ Object
Converts a number to binary byte units
For example,
LinuxStat::PrettifyBytes.convert_short_binary(1000)
> “1000 B”
LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3)
> “953.67 MiB”
LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3)
> “1.0 GiB”
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/linux_stat/prettify_bytes.rb', line 97 def convert_short_binary(n, precision: 2) return "#{pad_left(n, precision)} B" if n < 1024 @@sb_units ||= %W(#{''} K M G T P E Z) .map.with_index { |x, i| [x, 1024.**(i + 1)] } unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9] converted = n.fdiv(unit[1] / 1024).round(2) "#{pad_left(converted, precision)} #{unit[0]}iB" end |
.convert_short_decimal(n, precision: 2) ⇒ Object
Converts a number to decimal byte units For example,
LinuxStat::PrettifyBytes.convert_short_decimal(1000)
> “1.0 kB”
LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3)
> “1.0 GB”
LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3)
> “1.07 GB”
72 73 74 75 76 77 78 79 |
# File 'lib/linux_stat/prettify_bytes.rb', line 72 def convert_short_decimal(n, precision: 2) @@sd_units ||= %W(#{''} k M G T P E Z) .map.with_index { |x, i| [x, 1000.**(i + 1)] } unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27] converted = n.fdiv(unit[1] / 1000).round(2) "#{pad_left(converted, precision)} #{unit[0]}B" end |