mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix: remove scientific notation in DecimalBytes
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user