Module: Dratools::ByteFormatter
- Defined in:
- lib/dratools/byte_formatter.rb
Overview
Byte count formatter for human-readable IEC units.
Constant Summary collapse
- UNITS =
%w[B KiB MiB GiB TiB PiB].freeze
- UNIT_BASE =
1024.0
Class Method Summary collapse
Class Method Details
.format(bytes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dratools/byte_formatter.rb', line 11 def format(bytes) value = bytes.to_f unit_index = 0 while value >= UNIT_BASE && unit_index < UNITS.length - 1 value /= UNIT_BASE unit_index += 1 end return "#{bytes.to_i} B" if unit_index.zero? "#{Kernel.format('%.1f', value)} #{UNITS[unit_index]}" end |