Skip to content

Commit

Permalink
utils: update bytesize.Int64
Browse files Browse the repository at this point in the history
  • Loading branch information
spinlock committed Dec 22, 2017
1 parent b9272a9 commit ad6834b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/utils/bytesize/bytesize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bytesize

import (
"fmt"
"math"
"regexp"
"strconv"

Expand All @@ -31,6 +32,23 @@ func (b Int64) AsInt() int {
return int(b)
}

func (b Int64) HumanString() string {
switch abs := math.Abs(float64(b)); {
case abs > PB:
return fmt.Sprintf("%.2fpb", float64(b)/PB)
case abs > TB:
return fmt.Sprintf("%.2ftb", float64(b)/TB)
case abs > GB:
return fmt.Sprintf("%.2fgb", float64(b)/GB)
case abs > MB:
return fmt.Sprintf("%.2fmb", float64(b)/MB)
case abs > KB:
return fmt.Sprintf("%.2fkb", float64(b)/KB)
default:
return fmt.Sprintf("%d", b.Int64())
}
}

func (b Int64) MarshalText() ([]byte, error) {
if b == 0 {
return []byte("0"), nil
Expand Down

0 comments on commit ad6834b

Please sign in to comment.