diff --git a/internal/ui/format.go b/internal/ui/format.go index dc0166089..13d02f9e3 100644 --- a/internal/ui/format.go +++ b/internal/ui/format.go @@ -8,13 +8,13 @@ import ( func FormatBytes(c uint64) string { b := float64(c) switch { - case c > 1<<40: + case c >= 1<<40: return fmt.Sprintf("%.3f TiB", b/(1<<40)) - case c > 1<<30: + case c >= 1<<30: return fmt.Sprintf("%.3f GiB", b/(1<<30)) - case c > 1<<20: + case c >= 1<<20: return fmt.Sprintf("%.3f MiB", b/(1<<20)) - case c > 1<<10: + case c >= 1<<10: return fmt.Sprintf("%.3f KiB", b/(1<<10)) default: return fmt.Sprintf("%d B", c) diff --git a/internal/ui/format_test.go b/internal/ui/format_test.go index 3be08d496..b6a1c13d1 100644 --- a/internal/ui/format_test.go +++ b/internal/ui/format_test.go @@ -8,8 +8,13 @@ func TestFormatBytes(t *testing.T) { want string }{ {0, "0 B"}, - {1025, "1.001 KiB"}, - {1<<30 + 7, "1.000 GiB"}, + {1023, "1023 B"}, + {1024, "1.000 KiB"}, + {5<<20 + 1<<19, "5.500 MiB"}, + {1 << 30, "1.000 GiB"}, + {2 << 30, "2.000 GiB"}, + {1<<40 - 1<<36, "960.000 GiB"}, + {1 << 40, "1.000 TiB"}, } { if got := FormatBytes(c.size); got != c.want { t.Errorf("want %q, got %q", c.want, got)