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