aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs37
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");
}
}