From b447b6cf0ab35b80fc94f5000c2299ee5680cb50 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 5 Nov 2019 10:50:27 -0800 Subject: [PATCH] only trim zeros to the right of the decimal --- chain/types/bigint_test.go | 17 +++++++++++++++++ chain/types/fil.go | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/chain/types/bigint_test.go b/chain/types/bigint_test.go index 7e2cd71f4..5209e7051 100644 --- a/chain/types/bigint_test.go +++ b/chain/types/bigint_test.go @@ -32,3 +32,20 @@ func TestBigIntSerializationRoundTrip(t *testing.T) { } } + +func TestFilRoundTrip(t *testing.T) { + testValues := []string{ + "0", "1", "1.001", "100.10001", "101100", "5000.01", "5000", + } + + for _, v := range testValues { + fval, err := ParseFIL(v) + if err != nil { + t.Fatal(err) + } + + if fval.String() != v { + t.Fatal("mismatch in values!", v, fval.String()) + } + } +} diff --git a/chain/types/fil.go b/chain/types/fil.go index f44336fb5..80de6ced3 100644 --- a/chain/types/fil.go +++ b/chain/types/fil.go @@ -12,7 +12,10 @@ 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.") + if r.Sign() == 0 { + return "0" + } + return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".") } func (f FIL) Format(s fmt.State, ch rune) {