fix tests, handle parsing with suffixes properly

This commit is contained in:
whyrusleeping 2020-07-09 17:26:06 -07:00
parent a79b31c230
commit a3602d4fcf
2 changed files with 3 additions and 2 deletions

View File

@ -43,7 +43,7 @@ func TestBigIntSerializationRoundTrip(t *testing.T) {
func TestFilRoundTrip(t *testing.T) { func TestFilRoundTrip(t *testing.T) {
testValues := []string{ testValues := []string{
"0", "1", "1.001", "100.10001", "101100", "5000.01", "5000", "0 FIL", "1 FIL", "1.001 FIL", "100.10001 FIL", "101100 FIL", "5000.01 FIL", "5000 FIL",
} }
for _, v := range testValues { for _, v := range testValues {

View File

@ -13,7 +13,7 @@ type FIL BigInt
func (f FIL) String() string { func (f FIL) String() string {
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(build.FilecoinPrecision))) r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(build.FilecoinPrecision)))
if r.Sign() == 0 { if r.Sign() == 0 {
return "0" return "0 FIL"
} }
return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".") + " FIL" return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".") + " FIL"
} }
@ -29,6 +29,7 @@ func (f FIL) Format(s fmt.State, ch rune) {
func ParseFIL(s string) (FIL, error) { func ParseFIL(s string) (FIL, error) {
suffix := strings.TrimLeft(s, ".1234567890") suffix := strings.TrimLeft(s, ".1234567890")
s = s[:len(s)-len(suffix)]
var attofil bool var attofil bool
if suffix != "" { if suffix != "" {
norm := strings.ToLower(strings.TrimSpace(suffix)) norm := strings.ToLower(strings.TrimSpace(suffix))