human_bytes
is a Rust crate to convert bytes into human-readable values.
Add this to your Cargo.toml
:
toml
[dependencies]
human_bytes = "0.1"
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>
```
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"
The function uses f64
floating point numbers,
which ranges from $±0000000000000000×10^{−398}
$ to $±9999999999999999×10^{369}
$ acording to Wikipedia
That's a number so big, that running human_bytes(std::f64::MAX)
panic!
's with 'index out of bounds: the len is 9 but the index is 102'
.
That's because I only have suffixes up to yottabytes, which quoting Wikipedia
In 2010, it was estimated that storing a yottabyte on terabyte-size disk drives would require one million city block-size data-centers, as big as the states of Delaware and Rhode Island combined.[1] By late 2016 memory density had increased to the point where a yottabyte could be stored on SD cards occupying roughly twice the size of the Hindenburg[2] (around 400 thousand cubic metres). so I decided not to go bigger (and anyway, the SI doesn't define bigger prefixes).
I use f64
to be able of returning decimals (e.g. 5.3 GB
), but if you want to use u64
bytes, you have to write 10_u64 as f64
, because f64
dousn't implement From<u64>
.
And no way you're going to use u128
's. Anyway, human_bytes(std::u64::MAX as f64)
returns 16 EB
, which is more than Google has on their servers (source).
For now, I only test correct conversions up to 2.5 terabytes.
BSD 2-clause - Copyright (c) 2020 Namkhai Bourquin (forkbomb9)