https://github.com/sysrqmagician/bittenhumans
A lightweight, simple byte size humanization library for Rust.
https://github.com/sysrqmagician/bittenhumans
file-size humanization library
Last synced: 2 months ago
JSON representation
A lightweight, simple byte size humanization library for Rust.
- Host: GitHub
- URL: https://github.com/sysrqmagician/bittenhumans
- Owner: sysrqmagician
- License: mit
- Created: 2025-04-05T19:29:40.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-10-16T04:50:34.000Z (6 months ago)
- Last Synced: 2025-11-22T09:02:14.897Z (5 months ago)
- Topics: file-size, humanization, library
- Language: Rust
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# bittenhumans
[](https://crates.io/crates/bittenhumans)

[](https://docs.rs/bittenhumans)
A lightweight, simple byte size humanization library for Rust.
## Features
- Basic humanization, supporting both **decimal** (KB, MB, GB) and **binary** (KiB, MiB, GiB) numeral systems
- Automatically fit values and reuse formatters
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
bittenhumans = "1.0.0"
```
## Usage
### Basic Usage
```rust
use bittenhumans::ByteSizeFormatter;
use bittenhumans::consts::System;
fn main() {
// Format with automatic unit selection
let formatted = ByteSizeFormatter::format_auto(1_500_000, System::Decimal);
assert_eq!(formatted, "1.50 MB");
// Format using binary system (powers of 1024)
let formatted = ByteSizeFormatter::format_auto(1_500_000, System::Binary);
assert_eq!(formatted, "1.43 MiB");
}
```
### Creating Reusable Formatters
```rust
use bittenhumans::ByteSizeFormatter;
use bittenhumans::consts::{Magnitude, System};
fn main() {
// Create a formatter for gigabytes
let gb_formatter = ByteSizeFormatter::new(System::Decimal, Magnitude::Giga);
// Use it multiple times with different values
let total_space = gb_formatter.format_value(1_000_000_000_000); // "1000.00 GB"
let free_space = gb_formatter.format_value(250_000_000_000); // "250.00 GB"
}
```
### Creating Size-Appropriate Formatters
```rust
use bittenhumans::ByteSizeFormatter;
use bittenhumans::consts::System;
fn main() {
// Create a formatter that fits the largest value you'll format
let disk_size = 2_000_000_000_000; // 2 TB
let formatter = ByteSizeFormatter::fit(disk_size, System::Binary);
// All values will use the same unit for consistent display
println!("Disk size: {}", formatter.format_value(disk_size)); // "1.82 TiB"
println!("Used space: {}", formatter.format_value(disk_size/4)); // "0.45 TiB"
}
```
## Documentation
For more detailed information, check the [API documentation](https://docs.rs/bittenhumans).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.