fix(size): move byte sizes to the next unit when rounding reaches 1024

This commit is contained in:
kevin committed 2026-11-01 12:01:31 +09:00
1 parent 1a4a8e2366
commit de34556d42
4 files changed
+34 -2

No files matched your search

+3 -1
View File
@@ -775,7 +775,9 @@ func humanSize(b int64) string {
_MiB = 1024 * _KiB
)
switch {
case b >= _MiB:
// From _MiB-51 bytes up, the KiB figure rounds to "1024.0" at one decimal
// place, so show it as MiB instead.
case b >= _MiB-51:
return fmt.Sprintf("%.1f MiB", float64(b)/float64(_MiB))
case b >= _KiB:
return fmt.Sprintf("%.1f KiB", float64(b)/float64(_KiB))
+6
View File
@@ -206,6 +206,12 @@ func TestHumanSize(t *testing.T) {
{500, "500 B"},
{2048, "2.0 KiB"},
{19_000_000, "18.1 MiB"},
// Sizes just under the next unit round up to it: show "1.0 MiB",
// never "1024.0 KiB".
{1_048_524, "1023.9 KiB"},
{1_048_525, "1.0 MiB"},
{1_048_575, "1.0 MiB"},
{1_023, "1023 B"},
}
for _, tt := range tests {
if got := humanSize(tt.bytes); got != tt.want {
+3 -1
View File
@@ -339,7 +339,9 @@ func stillRunning(p *jobs.Progress) string {
func byteCount(n int64) string {
switch {
case n >= 1<<20:
// From 1<<20 - 51 bytes up, the KB figure rounds to "1024.0" at one
// decimal place, so show it as MB instead.
case n >= 1<<20-51:
return fmt.Sprintf("%.1f MB", float64(n)/(1<<20))
case n >= 1<<10:
return fmt.Sprintf("%.1f KB", float64(n)/(1<<10))
@@ -0,0 +1,22 @@
package builtin
import "testing"
func TestByteCountMovesToNextUnitWhenRoundingReachesIt(t *testing.T) {
tests := []struct {
n int64
want string
}{
{1023, "1023 B"},
{1024, "1.0 KB"},
{1_048_524, "1023.9 KB"},
{1_048_525, "1.0 MB"},
{1_048_575, "1.0 MB"},
{1 << 20, "1.0 MB"},
}
for _, tt := range tests {
if got := byteCount(tt.n); got != tt.want {
t.Errorf("byteCount(%d) = %q, want %q", tt.n, got, tt.want)
}
}
}