lotus/cli/utils.go
Jakub Sztandera 1124f8d994
Add test cases
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
2019-12-10 21:33:27 +01:00

25 lines
460 B
Go

package cli
import (
"fmt"
"math/big"
"github.com/filecoin-project/lotus/chain/types"
)
var Units = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"}
func SizeStr(size types.BigInt) string {
r := new(big.Rat).SetInt(size.Int)
den := big.NewRat(1, 1024)
var i int
for f, _ := r.Float64(); f >= 1024 && 1 < len(Units); f, _ = r.Float64() {
i++
r = r.Mul(r, den)
}
f, _ := r.Float64()
return fmt.Sprintf("%.3g %s", f, Units[i])
}