human_bytes
is a Rust crate that converts bytes into human-readable values (KB, MB, etc).
Add to your Cargo.toml
:
toml
[dependencies]
human_bytes = "0.2"
And then
```rust use humanbytes::humanbytes;
asserteq!(humanbytes(563200u32), "550 KB".tostring());
// or
asserteq!(humanbytes(563200u64 as f64), "550 KB".tostring());
// __________/
// |
// | Needed only when you're using u64
values,
// | because f64
doesn't implement std::convert::From<u64>
```
human_bytes
is dependency-free, but if you want an +/- 15% speed improvement, I have a fast
feature (which depends on lexical)
toml
[dependencies]
human_bytes = { version = "0.2", features = [ "fast" ] }
This crate 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. This means it'll return the correct prefix, and never return "1000 KB", always "1 MB"
It has some tests I wrote to check that the conversion is correct, and it returns decimals (e.g. 16.5 GB
)
Check the CHANGELOG.md
BSD 2-clause - Copyright (c) 2020 Namkhai B. (forkbomb9)