lotus/chain/types/fil.go
Jakub Sztandera 237df4cc44
New big rats and set the precision to 18 (atto = 1e-18)
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
2019-10-18 23:45:15 +09:00

31 lines
592 B
Go

package types
import (
"fmt"
"math/big"
"strings"
"github.com/filecoin-project/lotus/build"
)
type FIL BigInt
func (f FIL) String() string {
r := new(big.Rat).SetFrac(f.Int, big.NewInt(build.FilecoinPrecision))
return strings.TrimRight(r.FloatString(18), "0")
}
func ParseFIL(s string) (FIL, error) {
r, ok := new(big.Rat).SetString(s)
if !ok {
return FIL{}, fmt.Errorf("failed to parse %q as a decimal number", s)
}
r = r.Mul(r, big.NewRat(build.FilecoinPrecision, 1))
if !r.IsInt() {
return FIL{}, fmt.Errorf("invalid FIL value: %q", s)
}
return FIL{r.Num()}, nil
}