CI Crate

Humanize Bytes

Create a human-readable string representation of a number of bytes either in binary or decimal units (SI or IEC).

https://en.wikipedia.org/wiki/Binary_prefix

1 KB = 1000 B 1 KiB = 1024 B

```rust use humanizebytes::{humanizebytesdecimal, humanizebytesbinary, humanizequantity};

asserteq!(humanizebytesbinary!(0), "0 B"); asserteq!(humanizebytesbinary!(512), "512 B"); asserteq!(humanizebytesbinary!(1023), "1023 B"); asserteq!(humanizebytesbinary!(1024), "1 KiB"); asserteq!(humanizebytesbinary!(1024 + 99), "1 KiB"); asserteq!(humanizebytesbinary!(1024 + 103), "1.1 KiB"); asserteq!(humanizebytesbinary!(1024 * 1024 - 1), "1023.9 KiB"); asserteq!(humanizebytesbinary!(1024 * 1024), "1 MiB"); asserteq!(humanizebytes_binary!(1024 * 1024 * 1024), "1 GiB");

asserteq!(humanizebytesdecimal!(0), "0 B"); asserteq!(humanizebytesdecimal!(512), "512 B"); asserteq!(humanizebytesdecimal!(999), "999 B"); asserteq!(humanizebytesdecimal!(1000), "1 kB"); asserteq!(humanizebytesdecimal!(1000 + 99), "1 kB"); asserteq!(humanizebytesdecimal!(1000 + 100), "1.1 kB"); asserteq!(humanizebytesdecimal!(1000 * 1000 - 1), "999.9 kB"); asserteq!(humanizebytesdecimal!(1000 * 1000), "1 MB"); asserteq!(humanizebytes_decimal!(1000 * 1000 * 1000), "1 GB");

asserteq!(humanizequantity!(0), "0"); asserteq!(humanizequantity!(512), "512"); asserteq!(humanizequantity!(999), "999"); asserteq!(humanizequantity!(1000), "1 k"); asserteq!(humanizequantity!(1000 + 99), "1 k"); asserteq!(humanizequantity!(1000 + 100), "1.1 k"); asserteq!(humanizequantity!(1000 * 1000 - 1), "999.9 k"); asserteq!(humanizequantity!(1000 * 1000), "1 M"); ```

Implementation Details

This crate has one dependency, smartstring, and does not allocate because all formatting fits within the MAX_INLINE limit for a SmartString<LazyCompact>. Both macros return a SmartString<LazyCompact>, which looks/feels just like a normal String.