8 个版本 (稳定)
1.0.6 | 2024年4月8日 |
---|---|
1.0.5 | 2023年3月27日 |
1.0.4 | 2023年3月23日 |
1.0.3 | 2023年3月22日 |
0.1.0 | 2023年3月22日 |
#55 在 值格式化
每月536 次下载
在 2 crate 中使用
12KB
139 行
Humanize Bytes
以二进制或十进制单位(SI或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");
实现细节
此 crate 有一个依赖项,smartstring
,且不进行内存分配,因为所有格式化都在 MAX_INLINE
限制内,适用于 SmartString<LazyCompact>
。这两个宏都返回一个 SmartString<LazyCompact>
,看起来/感觉就像一个正常的 String
。
依赖项
~135KB