Crates.io | human_bytes |
lib.rs | human_bytes |
version | 0.4.3 |
source | src |
created_at | 2020-02-07 01:11:52.711055 |
updated_at | 2023-09-10 19:41:00.533106 |
description | Crate to convert bytes into human-readable values |
homepage | https://sr.ht/~nkeor/human_bytes |
repository | https://git.sr.ht/~nkeor/human_bytes |
max_upload_size | |
id | 205628 |
size | 13,846 |
A Rust crate & cli to convert bytes into human-readable values.
It can return either KiB/MiB/GiB/TiB or KB/MB/GB/TB by disabling the si-units
feature.
1 KiB = 1024 B, 1 KB = 1000 B
It supports from 0 bytes to several yottabytes (I cannot tell how many because I have to use u128
s
to fit a single YB)
just build-binary
cargo build --release --features 'build-binary fast' --bin hb
target/release/hb
to somewhere in your $PATH
hb <bytes>
or echo <bytes> | hb
Add to your Cargo.toml
:
[dependencies]
human_bytes = "0.4"
# or, to disable the SI Units:
human_bytes = { version = "0.4", default-features = false }
And then
use human_bytes::human_bytes;
assert_eq!(human_bytes(563_200_u32), "550 KiB".to_string());
// or
assert_eq!(human_bytes(563_200_u64 as f64), "550 KiB".to_string());
// ________________________________/
// |
// | Needed only when you're using `u64` values,
// | because `f64` doesn't implement `std::convert::From<u64>`
// With the `si-units` feature disabled:
assert_eq!(human_bytes(550_000_u32), "550 KB".to_string());
The crate is dependency-free, but you can boost the speed by enabling the fast
feature,
which switches from using std::format!
to ryu
to convert floats to strings.
[dependencies]
human_bytes = { version = "0.4", features = ["fast"] }
The code is based on a PHP function I found here.
It is useful because you don't have to provide a prefix, it does it on its own.
It'll always return 1 MiB
instead of 1024 KiB
It has some tests I wrote to check that the conversion is correct, and it returns decimals (e.g. 16.5 GiB
)
Check the CHANGELOG.md
BSD 2-clause (c) 2020-2022 Namkhai B.