8 releases (stable)

1.0.6 Apr 8, 2024
1.0.5 Mar 27, 2023
1.0.4 Mar 23, 2023
1.0.3 Mar 22, 2023
0.1.0 Mar 22, 2023

#55 in Value formatting

Download history 17/week @ 2024-09-03 6/week @ 2024-09-10 25/week @ 2024-09-17 59/week @ 2024-09-24 43/week @ 2024-10-01 62/week @ 2024-10-08 22/week @ 2024-10-15 15/week @ 2024-10-22 43/week @ 2024-10-29 75/week @ 2024-11-05 45/week @ 2024-11-12 53/week @ 2024-11-19 45/week @ 2024-11-26 66/week @ 2024-12-03 114/week @ 2024-12-10 302/week @ 2024-12-17

536 downloads per month
Used in 4 crates

MIT/Apache

12KB
139 lines

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

use humanize_bytes::{humanize_bytes_decimal, humanize_bytes_binary, humanize_quantity};
 
assert_eq!(humanize_bytes_binary!(0), "0 B");
assert_eq!(humanize_bytes_binary!(512), "512 B");
assert_eq!(humanize_bytes_binary!(1023), "1023 B");
assert_eq!(humanize_bytes_binary!(1024), "1 KiB");
assert_eq!(humanize_bytes_binary!(1024 + 99), "1 KiB");
assert_eq!(humanize_bytes_binary!(1024 + 103), "1.1 KiB");
assert_eq!(humanize_bytes_binary!(1024 * 1024 - 1), "1023.9 KiB");
assert_eq!(humanize_bytes_binary!(1024 * 1024), "1 MiB");
assert_eq!(humanize_bytes_binary!(1024 * 1024 * 1024), "1 GiB");

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

assert_eq!(humanize_quantity!(0), "0");
assert_eq!(humanize_quantity!(512), "512");
assert_eq!(humanize_quantity!(999), "999");
assert_eq!(humanize_quantity!(1000), "1 k");
assert_eq!(humanize_quantity!(1000 + 99), "1 k");
assert_eq!(humanize_quantity!(1000 + 100), "1.1 k");
assert_eq!(humanize_quantity!(1000 * 1000 - 1), "999.9 k");
assert_eq!(humanize_quantity!(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.

Dependencies

~135KB