2019-10-18 12:31:45 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FIL BigInt
|
|
|
|
|
|
|
|
func (f FIL) String() string {
|
2020-06-30 13:18:01 +00:00
|
|
|
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(build.FilecoinPrecision)))
|
2019-11-05 18:50:27 +00:00
|
|
|
if r.Sign() == 0 {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".")
|
2019-10-18 12:31:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:52:24 +00:00
|
|
|
func (f FIL) Format(s fmt.State, ch rune) {
|
|
|
|
switch ch {
|
|
|
|
case 's', 'v':
|
|
|
|
fmt.Fprint(s, f.String())
|
|
|
|
default:
|
|
|
|
f.Int.Format(s, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:31:45 +00:00
|
|
|
func ParseFIL(s string) (FIL, error) {
|
2019-10-18 14:45:15 +00:00
|
|
|
r, ok := new(big.Rat).SetString(s)
|
2019-10-18 12:31:45 +00:00
|
|
|
if !ok {
|
|
|
|
return FIL{}, fmt.Errorf("failed to parse %q as a decimal number", s)
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:18:01 +00:00
|
|
|
r = r.Mul(r, big.NewRat(int64(build.FilecoinPrecision), 1))
|
2019-10-18 12:31:45 +00:00
|
|
|
if !r.IsInt() {
|
|
|
|
return FIL{}, fmt.Errorf("invalid FIL value: %q", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return FIL{r.Num()}, nil
|
|
|
|
}
|