lotus/cli/utils.go

25 lines
460 B
Go
Raw Normal View History

2019-12-10 18:04:13 +00:00
package cli
import (
"fmt"
"math/big"
2019-12-10 18:04:13 +00:00
"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() {
2019-12-10 18:04:13 +00:00
i++
r = r.Mul(r, den)
2019-12-10 18:04:13 +00:00
}
f, _ := r.Float64()
return fmt.Sprintf("%.3g %s", f, Units[i])
2019-12-10 18:04:13 +00:00
}