From ab9b3b685163f9944f0ca00c347a0d3f6093113b Mon Sep 17 00:00:00 2001 From: He Weidong Date: Sat, 9 Jan 2021 17:48:36 +0800 Subject: [PATCH] enable parse and shorten negative FIL values --- chain/types/fil.go | 4 ++-- chain/types/fil_test.go | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/chain/types/fil.go b/chain/types/fil.go index 6742dd18000..223ed3c5095 100644 --- a/chain/types/fil.go +++ b/chain/types/fil.go @@ -26,7 +26,7 @@ func (f FIL) Unitless() string { var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m"} func (f FIL) Short() string { - n := BigInt(f) + n := BigInt(f).Abs() dn := uint64(1) var prefix string @@ -70,7 +70,7 @@ func (f FIL) UnmarshalText(text []byte) 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 if suffix != "" { diff --git a/chain/types/fil_test.go b/chain/types/fil_test.go index 6cbc44c5f9b..7bf2a802ede 100644 --- a/chain/types/fil_test.go +++ b/chain/types/fil_test.go @@ -57,6 +57,52 @@ func TestFilShort(t *testing.T) { {fil: "0.000221234", expect: "221.234 μFIL"}, {fil: "0.0002212344", expect: "221.234 μFIL"}, {fil: "0.00022123444", expect: "221.234 μFIL"}, + + {fil: "-1", expect: "-1 FIL"}, + {fil: "-1.1", expect: "-1.1 FIL"}, + {fil: "-12", expect: "-12 FIL"}, + {fil: "-123", expect: "-123 FIL"}, + {fil: "-123456", expect: "-123456 FIL"}, + {fil: "-123.23", expect: "-123.23 FIL"}, + {fil: "-123456.234", expect: "-123456.234 FIL"}, + {fil: "-123456.2341234", expect: "-123456.234 FIL"}, + {fil: "-123456.234123445", expect: "-123456.234 FIL"}, + + {fil: "-0.1", expect: "-100 mFIL"}, + {fil: "-0.01", expect: "-10 mFIL"}, + {fil: "-0.001", expect: "-1 mFIL"}, + + {fil: "-0.0001", expect: "-100 μFIL"}, + {fil: "-0.00001", expect: "-10 μFIL"}, + {fil: "-0.000001", expect: "-1 μFIL"}, + + {fil: "-0.0000001", expect: "-100 nFIL"}, + {fil: "-0.00000001", expect: "-10 nFIL"}, + {fil: "-0.000000001", expect: "-1 nFIL"}, + + {fil: "-0.0000000001", expect: "-100 pFIL"}, + {fil: "-0.00000000001", expect: "-10 pFIL"}, + {fil: "-0.000000000001", expect: "-1 pFIL"}, + + {fil: "-0.0000000000001", expect: "-100 fFIL"}, + {fil: "-0.00000000000001", expect: "-10 fFIL"}, + {fil: "-0.000000000000001", expect: "-1 fFIL"}, + + {fil: "-0.0000000000000001", expect: "-100 aFIL"}, + {fil: "-0.00000000000000001", expect: "-10 aFIL"}, + {fil: "-0.000000000000000001", expect: "-1 aFIL"}, + + {fil: "-0.0000012", expect: "-1.2 μFIL"}, + {fil: "-0.00000123", expect: "-1.23 μFIL"}, + {fil: "-0.000001234", expect: "-1.234 μFIL"}, + {fil: "-0.0000012344", expect: "-1.234 μFIL"}, + {fil: "-0.00000123444", expect: "-1.234 μFIL"}, + + {fil: "-0.0002212", expect: "-221.2 μFIL"}, + {fil: "-0.00022123", expect: "-221.23 μFIL"}, + {fil: "-0.000221234", expect: "-221.234 μFIL"}, + {fil: "-0.0002212344", expect: "-221.234 μFIL"}, + {fil: "-0.00022123444", expect: "-221.234 μFIL"}, } { s := s t.Run(s.fil, func(t *testing.T) {