| Crates.io | bittenhumans |
| lib.rs | bittenhumans |
| version | 1.0.0 |
| created_at | 2024-05-07 08:03:26.635702+00 |
| updated_at | 2025-04-05 19:45:33.767572+00 |
| description | Simple byte size humanization library for stringbar |
| homepage | |
| repository | https://github.com/sysrqmagician/bittenhumans |
| max_upload_size | |
| id | 1231945 |
| size | 13,414 |
A lightweight, simple byte size humanization library for Rust.
Add this to your Cargo.toml:
[dependencies]
bittenhumans = "1.0.0"
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");
}
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"
}
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"
}
For more detailed information, check the API documentation.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.