forked from TrueCloudLab/restic
ui: Fix FormatBytes at exactly 1024 time a unit
1024 would be displayed as "1024 bytes" instead of "1.000 KiB", etc.
This commit is contained in:
parent
006380199e
commit
5ab3e6276a
2 changed files with 11 additions and 6 deletions
|
@ -8,13 +8,13 @@ import (
|
||||||
func FormatBytes(c uint64) string {
|
func FormatBytes(c uint64) string {
|
||||||
b := float64(c)
|
b := float64(c)
|
||||||
switch {
|
switch {
|
||||||
case c > 1<<40:
|
case c >= 1<<40:
|
||||||
return fmt.Sprintf("%.3f TiB", b/(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))
|
return fmt.Sprintf("%.3f GiB", b/(1<<30))
|
||||||
case c > 1<<20:
|
case c >= 1<<20:
|
||||||
return fmt.Sprintf("%.3f MiB", b/(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))
|
return fmt.Sprintf("%.3f KiB", b/(1<<10))
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d B", c)
|
return fmt.Sprintf("%d B", c)
|
||||||
|
|
|
@ -8,8 +8,13 @@ func TestFormatBytes(t *testing.T) {
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{0, "0 B"},
|
{0, "0 B"},
|
||||||
{1025, "1.001 KiB"},
|
{1023, "1023 B"},
|
||||||
{1<<30 + 7, "1.000 GiB"},
|
{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 {
|
if got := FormatBytes(c.size); got != c.want {
|
||||||
t.Errorf("want %q, got %q", c.want, got)
|
t.Errorf("want %q, got %q", c.want, got)
|
||||||
|
|
Loading…
Reference in a new issue