File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ package bytesize
5
5
6
6
import (
7
7
"fmt"
8
+ "math"
8
9
"regexp"
9
10
"strconv"
10
11
@@ -31,6 +32,23 @@ func (b Int64) AsInt() int {
31
32
return int (b )
32
33
}
33
34
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
+
34
52
func (b Int64 ) MarshalText () ([]byte , error ) {
35
53
if b == 0 {
36
54
return []byte ("0" ), nil
You can’t perform that action at this time.
0 commit comments