diff options
| author | Joris Guyonvarch | 2026-01-16 19:25:37 +0100 |
|---|---|---|
| committer | Joris Guyonvarch | 2026-01-16 19:25:37 +0100 |
| commit | c90bd8eba71539a8cd58fe865de5695813a57fbd (patch) | |
| tree | 87b55c66e0e34e7755d92f61e9821a0e8bf5c6bc /src | |
| parent | 8f9e9c0e5142b2a224dda926396149b7e1dd4602 (diff) | |
Augment precision of file size
Diffstat (limited to 'src')
| -rw-r--r-- | src/util.rs | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/util.rs b/src/util.rs index 9bc7cb9..27280b2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -36,15 +36,16 @@ pub fn pretty_print_duration(d: Duration) -> String { } pub fn pretty_print_bytes(bytes: usize) -> String { - let ko = bytes / 1024; - let mo = ko / 1024; - let go = mo / 1024; - if go > 0 { - format!("{} GB", go) - } else if mo > 0 { - format!("{} MB", mo) - } else if ko > 0 { - format!("{} KB", ko) + let bytes_f32 = bytes as f32; + let ko = bytes_f32 / 1024.0; + let mo = ko / 1024.0; + let go = mo / 1024.0; + if go >= 1.0 { + format!("{:.1} G", go) + } else if mo >= 1.0 { + format!("{:.1} M", mo) + } else if ko >= 1.0 { + format!("{:.1} K", ko) } else { format!("{} B", bytes) } @@ -112,14 +113,14 @@ mod tests { fn test_pretty_print_bytes() { assert_eq!(pretty_print_bytes(0), "0 B"); assert_eq!(pretty_print_bytes(10), "10 B"); - assert_eq!(pretty_print_bytes(1024), "1 KB"); - assert_eq!(pretty_print_bytes(1100), "1 KB"); - assert_eq!(pretty_print_bytes(54 * 1024), "54 KB"); - assert_eq!(pretty_print_bytes(1024 * 1024), "1 MB"); - assert_eq!(pretty_print_bytes(1300 * 1024), "1 MB"); - assert_eq!(pretty_print_bytes(79 * 1024 * 1024), "79 MB"); - assert_eq!(pretty_print_bytes(1024 * 1024 * 1024), "1 GB"); - assert_eq!(pretty_print_bytes(1300 * 1024 * 1024), "1 GB"); - assert_eq!(pretty_print_bytes(245 * 1024 * 1024 * 1024), "245 GB"); + assert_eq!(pretty_print_bytes(1024), "1.0 K"); + assert_eq!(pretty_print_bytes(1100), "1.1 K"); + assert_eq!(pretty_print_bytes(54 * 1024), "54.0 K"); + assert_eq!(pretty_print_bytes(1024 * 1024), "1.0 M"); + assert_eq!(pretty_print_bytes(1250 * 1024), "1.2 M"); + assert_eq!(pretty_print_bytes(79 * 1024 * 1024), "79.0 M"); + assert_eq!(pretty_print_bytes(1024 * 1024 * 1024), "1.0 G"); + assert_eq!(pretty_print_bytes(1300 * 1024 * 1024), "1.3 G"); + assert_eq!(pretty_print_bytes(245 * 1024 * 1024 * 1024), "245.0 G"); } } |
