Skip to content

Commit 65e735f

Browse files
committed
unit: Add ByteSizeFormatFloat
1 parent d6ebdbe commit 65e735f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

unit/bytesize.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package unit
33
import (
44
"encoding"
55
"errors"
6-
"fmt"
76
"regexp"
87
"strconv"
98
"strings"
@@ -70,7 +69,13 @@ func NewByteSize(n float64, unit ByteSize) ByteSize {
7069
return ByteSize(n * float64(unit))
7170
}
7271

73-
var BytesFormat = "%.2g"
72+
func DefaultByteSizeFormatFloat(f float64) string {
73+
s := strconv.FormatFloat(f, 'f', 2, 64)
74+
s = strings.TrimRight(s, "0")
75+
return strings.TrimSuffix(s, ".")
76+
}
77+
78+
var ByteSizeFormatFloat = DefaultByteSizeFormatFloat
7479

7580
func (n ByteSize) String() string {
7681
unit := B
@@ -88,7 +93,7 @@ func (n ByteSize) String() string {
8893
case n >= KB:
8994
unit = KB
9095
}
91-
return fmt.Sprintf(BytesFormat+byteSizeStr[unit], float64(n)/float64(unit))
96+
return ByteSizeFormatFloat(float64(n)/float64(unit)) + byteSizeStr[unit]
9297
}
9398

9499
func (b ByteSize) MarshalText() ([]byte, error) {

0 commit comments

Comments
 (0)