The project is in a healthy, maintained state
Format byte counts as human-readable strings (1.5 MB) and parse them back. Supports SI units (KB, MB, GB) and binary units (KiB, MiB, GiB) with configurable precision.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-human_size

Tests Gem Version Last updated

Bidirectional byte size formatting with SI and binary units

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-human_size"

Or install directly:

gem install philiprehberger-human_size

Usage

require "philiprehberger/human_size"

Philiprehberger::HumanSize.format(1_500_000)  # => "1.5 MB"

Formatting Bytes

Convert integer bytes to human-readable strings using SI units (base 1000) by default:

Philiprehberger::HumanSize.format(1_500_000)      # => "1.5 MB"
Philiprehberger::HumanSize.format(1_000_000_000)   # => "1 GB"
Philiprehberger::HumanSize.format(0)               # => "0 B"

Binary Units

Pass binary: true to use IEC binary units (base 1024) instead of SI:

Philiprehberger::HumanSize.format(1_572_864, binary: true)  # => "1.5 MiB"
Philiprehberger::HumanSize.format(1_048_576, binary: true)   # => "1 MiB"
Philiprehberger::HumanSize.format(5_368_709_120, binary: true) # => "5 GiB"

Precision Control

Use the precision option to control the number of decimal places (default is 2). Trailing zeros are stripped automatically:

Philiprehberger::HumanSize.format(1_500_000, precision: 0)  # => "2 MB"
Philiprehberger::HumanSize.format(1_234_567, precision: 3)  # => "1.235 MB"
Philiprehberger::HumanSize.format(1_500_000, precision: 4)  # => "1.5 MB"

Parsing

Parse human-readable byte strings back to integer byte counts. Parsing is case-insensitive and supports both SI and binary units:

Philiprehberger::HumanSize.parse("1.5 GB")   # => 1500000000
Philiprehberger::HumanSize.parse("500 KiB")  # => 512000
Philiprehberger::HumanSize.parse("1 TB")     # => 1000000000000
Philiprehberger::HumanSize.parse("2.5 MiB")  # => 2621440

Structured Output

Use format_parts to get the numeric value and unit separately:

Philiprehberger::HumanSize.format_parts(1_500_000)                  # => { value: 1.5, unit: "MB" }
Philiprehberger::HumanSize.format_parts(1_572_864, binary: true)    # => { value: 1.5, unit: "MiB" }
Philiprehberger::HumanSize.format_parts(0)                          # => { value: 0.0, unit: "B" }

Validation

Check if a string is a valid byte size without raising:

Philiprehberger::HumanSize.valid?("1.5 GB")   # => true
Philiprehberger::HumanSize.valid?("nope")      # => false
Philiprehberger::HumanSize.valid?(123)          # => false

API

Method Description
HumanSize.format(bytes, binary: false, precision: 2) Convert integer bytes to a human-readable string (SI or binary units)
HumanSize.format_parts(bytes, binary: false, precision: 2) Return a hash with :value (Float) and :unit (String)
HumanSize.parse(string) Parse a human-readable byte string back to an integer byte count
HumanSize.valid?(string) Check if a string is a valid parseable byte size

Development

bundle install
bundle exec rspec
bundle exec rubocop

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT