Skip to content

Commit ad6834b

Browse files
committed
utils: update bytesize.Int64
1 parent b9272a9 commit ad6834b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/utils/bytesize/bytesize.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package bytesize
55

66
import (
77
"fmt"
8+
"math"
89
"regexp"
910
"strconv"
1011

@@ -31,6 +32,23 @@ func (b Int64) AsInt() int {
3132
return int(b)
3233
}
3334

35+
func (b Int64) HumanString() string {
36+
switch abs := math.Abs(float64(b)); {
37+
case abs > PB:
38+
return fmt.Sprintf("%.2fpb", float64(b)/PB)
39+
case abs > TB:
40+
return fmt.Sprintf("%.2ftb", float64(b)/TB)
41+
case abs > GB:
42+
return fmt.Sprintf("%.2fgb", float64(b)/GB)
43+
case abs > MB:
44+
return fmt.Sprintf("%.2fmb", float64(b)/MB)
45+
case abs > KB:
46+
return fmt.Sprintf("%.2fkb", float64(b)/KB)
47+
default:
48+
return fmt.Sprintf("%d", b.Int64())
49+
}
50+
}
51+
3452
func (b Int64) MarshalText() ([]byte, error) {
3553
if b == 0 {
3654
return []byte("0"), nil

0 commit comments

Comments
 (0)