File tree 1 file changed +8
-3
lines changed
1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package unit
3
3
import (
4
4
"encoding"
5
5
"errors"
6
- "fmt"
7
6
"regexp"
8
7
"strconv"
9
8
"strings"
@@ -70,7 +69,13 @@ func NewByteSize(n float64, unit ByteSize) ByteSize {
70
69
return ByteSize (n * float64 (unit ))
71
70
}
72
71
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
74
79
75
80
func (n ByteSize ) String () string {
76
81
unit := B
@@ -88,7 +93,7 @@ func (n ByteSize) String() string {
88
93
case n >= KB :
89
94
unit = KB
90
95
}
91
- return fmt . Sprintf ( BytesFormat + byteSizeStr [ unit ], float64 (n )/ float64 (unit ))
96
+ return ByteSizeFormatFloat ( float64 (n )/ float64 (unit )) + byteSizeStr [ unit ]
92
97
}
93
98
94
99
func (b ByteSize ) MarshalText () ([]byte , error ) {
You can’t perform that action at this time.
0 commit comments