1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 00:32:27 +08:00

fix: remove scientific notation in DecimalBytes

This commit is contained in:
dudaodong
2024-07-29 11:42:56 +08:00
parent eb7cf76eae
commit d5752499bf
2 changed files with 22 additions and 3 deletions

View File

@@ -84,13 +84,26 @@ var (
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
// Play: https://go.dev/play/p/FPXs1suwRcs
func DecimalBytes(size float64, precision ...int) string {
p := 5
pointPosition := 4
if len(precision) > 0 {
p = precision[0] + 1
pointPosition = precision[0]
}
size, unit := calculateByteSize(size, 1000.0, decimalByteUnits)
return fmt.Sprintf("%.*g%s", p, size, unit)
format := fmt.Sprintf("%%.%df", pointPosition)
result := fmt.Sprintf(format, size)
for i := len(result); i > 0; i-- {
s := result[i-1]
if s == '0' || s == '.' {
result = result[:i-1]
} else {
break
}
}
return result + unit
}
// BinaryBytes returns a human-readable byte size under binary standard (base 1024)