fix(package): [formatter] use the next byte unit when rounding hits base (#425)

* fix(package): [formatter] use the next byte unit when rounding hits base

* test(package): [formatter] cover byte unit rollover on rounding
This commit is contained in:
Lee Sang Gyu authored and GitHub committed 2026-11-09 12:26:44 +08:00
1 parent 285e69edd7
commit 9c56f5f525
2 files changed
+24 -2

No files matched your search

+17 -2
View File
@@ -92,7 +92,7 @@ func DecimalBytes(size float64, precision ...int) string {
pointPosition = precision[0] pointPosition = precision[0]
} }
size, unit := calculateByteSize(size, 1000.0, decimalByteUnits) size, unit := calculateRoundedByteSize(size, 1000.0, decimalByteUnits, pointPosition)
return roundToToString(size, pointPosition) + unit return roundToToString(size, pointPosition) + unit
} }
@@ -106,7 +106,7 @@ func BinaryBytes(size float64, precision ...int) string {
pointPosition = precision[0] pointPosition = precision[0]
} }
size, unit := calculateByteSize(size, 1024.0, binaryByteUnits) size, unit := calculateRoundedByteSize(size, 1024.0, binaryByteUnits, pointPosition)
return roundToToString(size, pointPosition) + unit return roundToToString(size, pointPosition) + unit
} }
@@ -121,6 +121,21 @@ func calculateByteSize(size float64, base float64, byteUnits []string) (float64,
return size, byteUnits[i] return size, byteUnits[i]
} }
// calculateRoundedByteSize works like calculateByteSize, but moves to the next unit
// when the value rounds up to the base at the given (non-negative) precision (e.g. 999999 bytes
// with precision 2 gives 1MB instead of 1000KB).
func calculateRoundedByteSize(size float64, base float64, byteUnits []string, precision int) (float64, string) {
size, unit := calculateByteSize(size, base, byteUnits)
i := 0
for byteUnits[i] != unit {
i++
}
if precision >= 0 && i < len(byteUnits)-1 && mathutil.RoundToFloat(size, precision) >= base {
return size / base, byteUnits[i+1]
}
return size, unit
}
func roundToToString(x float64, max ...int) string { func roundToToString(x float64, max ...int) string {
pointPosition := 4 pointPosition := 4
if len(max) > 0 { if len(max) > 0 {
+7
View File
@@ -28,6 +28,10 @@ func TestDecimalBytes(t *testing.T) {
assert.Equal("4MB", DecimalBytes(4000000, 0)) assert.Equal("4MB", DecimalBytes(4000000, 0))
assert.Equal("4MB", DecimalBytes(4000000, 1)) assert.Equal("4MB", DecimalBytes(4000000, 1))
assert.Equal("1MB", DecimalBytes(999999, 2))
assert.Equal("1GB", DecimalBytes(999999999, 2))
assert.Equal("999.99KB", DecimalBytes(999990, 2))
} }
func TestBinaryBytes(t *testing.T) { func TestBinaryBytes(t *testing.T) {
@@ -43,6 +47,9 @@ func TestBinaryBytes(t *testing.T) {
assert.Equal("10KiB", BinaryBytes(10240, 0)) assert.Equal("10KiB", BinaryBytes(10240, 0))
assert.Equal("10KiB", BinaryBytes(10240, 1)) assert.Equal("10KiB", BinaryBytes(10240, 1))
assert.Equal("10KiB", BinaryBytes(10240, 2)) assert.Equal("10KiB", BinaryBytes(10240, 2))
assert.Equal("1MiB", BinaryBytes(1048575, 2))
assert.Equal("1023.99KiB", BinaryBytes(1048566, 2))
} }
func TestParseDecimalBytes(t *testing.T) { func TestParseDecimalBytes(t *testing.T) {