Format byte sizes into human-readable strings using multiple scale types. Supports decimal (SI), binary (IEC), and Windows-style units.
Inspired by the humansize crate from the Rust ecosystem.
Written in Zig. No dependencies.
- Decimal (SI):
kB,MB,GB, etc. (base 1000) - Binary (IEC):
KiB,MiB,GiB, etc. (base 1024) - Windows-style:
KB,MB,GB, etc. (base 1024, but uses SI labels) - Both short and long unit names
- Works with fixed buffers, writers, or allocators
- Accurate rounding to two decimal places
Run this command in the root of your Zig project:
zig fetch --save "git+https://github.com/ciathefed/sizeify"Add this snippet to your build.zig:
const sizeify = b.dependency("sizeify", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("sizeify", sizeify.module("sizeify"));const sizeify = @import("sizeify");
std.log.debug("{f}", .{sizeify.fmt(1536, .binary_short)});
// debug: 1.50 KiBAlso available:
formatBuf(size, scale, buffer)formatAlloc(size, scale, allocator)formatWriter(size, scale, writer)
| Bytes | Scale | Output |
|---|---|---|
| 0 | decimal_short | 0.00 B |
| 1500 | decimal_short | 1.50 kB |
| 1024 | binary_long | 1.00 Kibibytes |
| 1024 | windows_short | 1.00 KB |
| max usize | decimal_short | 18.45 EB (on 64-bit) |
Run with:
zig build testThis project is licensed under the MIT License