human_bytes
A Rust crate that converts bytes into human-readable values.
It can return either KB/MB/GB/TB or KiB/MiB/GiB/TiB via the bibytes
feature,
which enables the power of 2 unit system.
(1 KB = 1024 B, 1 KiB = 1024 B, the only thing that changes is the suffix).
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)
Usage
Add to your Cargo.toml
:
[]
= "0.3"
# Or
= { = "0.3", = ["bibytes"] }
And then
use human_bytes;
assert_eq!;
// or
assert_eq!;
// ________________________________/
// |
// | Needed only when you're using `u64` values,
// | because `f64` doesn't implement `std::convert::From<u64>`
// With the `bibytes` feature enabled:
assert_eq!;
The crate is dependency-free, but if you want an +/- 15% speed improvement, there's a fast
feature
that uses lexical instead of std::format!
in the number-to-string conversion
[]
= { = "0.3", = ["fast"] }
About
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 MB
instead of 1000 KB
It has some tests I wrote to check that the conversion is correct, and it returns decimals (e.g. 16.5 GB
)
Changelog
Check the CHANGELOG.md
License
BSD 2-clause (c) 2020 Namkhai B.