From 8f74460c1ba4837c587b2ee4709d8bb240ff26f5 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 30 Jul 2024 14:17:17 +0800 Subject: [PATCH] fix: remove scientific notation in DecimalBytes --- formatter/byte.go | 14 +++++++------- formatter/byte_test.go | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/formatter/byte.go b/formatter/byte.go index 7355035..26b81f7 100644 --- a/formatter/byte.go +++ b/formatter/byte.go @@ -92,18 +92,18 @@ func DecimalBytes(size float64, precision ...int) string { size, unit := calculateByteSize(size, 1000.0, decimalByteUnits) format := fmt.Sprintf("%%.%df", pointPosition) - result := fmt.Sprintf(format, size) + bytes := fmt.Sprintf(format, size) - for i := len(result); i > 0; i-- { - s := result[i-1] - if s == '0' || s == '.' { - result = result[:i-1] - } else { + for i := len(bytes); i > 0; i-- { + s := bytes[i-1] + if s != '0' && s != '.' { break } + + bytes = bytes[:i-1] } - return result + unit + return bytes + unit } // BinaryBytes returns a human-readable byte size under binary standard (base 1024) diff --git a/formatter/byte_test.go b/formatter/byte_test.go index 07d8482..dc4e97a 100644 --- a/formatter/byte_test.go +++ b/formatter/byte_test.go @@ -20,7 +20,6 @@ func TestDecimalBytes(t *testing.T) { assert.Equal("3.123PB", DecimalBytes(float64(3.123*UnitPB))) assert.Equal("4.123EB", DecimalBytes(float64(4.123*UnitEB))) assert.Equal("1EB", DecimalBytes(float64(1000*UnitPB))) - assert.Equal("62MB", DecimalBytes(61812496, 0)) assert.Equal("61.8MB", DecimalBytes(61812496, 1)) assert.Equal("401MB", DecimalBytes(401000000))