Pick human_bytes unit after rounding (#1075)

A size just under a unit boundary shows in the next unit, so 1,048,575 bytes reads "1.0 MB" and not "1024.0 KB".
This commit is contained in:
NetterSilver authored and GitHub committed 2026-11-07 13:29:28 +08:00
1 parent 804a05510d
commit dcba2f3c54
1 file changed
+14 -2
+14 -2
View File
@@ -139,14 +139,18 @@ pub struct Coverage {
pub next_offset: Option<u64>,
}
fn round1(v: f64) -> f64 {
(v * 10.0).round() / 10.0
}
/// A byte count in plain words: "512 B", "1.5 KB", "6.30 GB".
pub fn human_bytes(n: u64) -> String {
let n = n as f64;
if n < 1024.0 {
format!("{n} B")
} else if n < 1024.0 * 1024.0 {
} else if round1(n / 1024.0) < 1024.0 {
format!("{:.1} KB", n / 1024.0)
} else if n < 1024.0 * 1024.0 * 1024.0 {
} else if round1(n / 1024.0 / 1024.0) < 1024.0 {
format!("{:.1} MB", n / 1024.0 / 1024.0)
} else {
format!("{:.2} GB", n / 1024.0 / 1024.0 / 1024.0)
@@ -394,6 +398,14 @@ mod tests {
.unwrap()
}
#[test]
fn human_bytes_moves_to_next_unit_after_rounding() {
assert_eq!(human_bytes(1024), "1.0 KB");
assert_eq!(human_bytes(1_048_575), "1.0 MB");
assert_eq!(human_bytes(1024 * 1024 * 1024 - 1), "1.00 GB");
assert_eq!(human_bytes(1024 * 1024 * 1024), "1.00 GB");
}
#[test]
fn id_is_stable_and_well_formed() {
let a = sample();