enable parse and shorten negative FIL values

This commit is contained in:
He Weidong 2021-01-09 17:48:36 +08:00
parent 32dcfb4842
commit ab9b3b6851
2 changed files with 48 additions and 2 deletions

View File

@ -26,7 +26,7 @@ func (f FIL) Unitless() string {
var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m"} var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m"}
func (f FIL) Short() string { func (f FIL) Short() string {
n := BigInt(f) n := BigInt(f).Abs()
dn := uint64(1) dn := uint64(1)
var prefix string var prefix string
@ -70,7 +70,7 @@ func (f FIL) UnmarshalText(text []byte) error {
} }
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)] s = s[:len(s)-len(suffix)]
var attofil bool var attofil bool
if suffix != "" { if suffix != "" {

View File

@ -57,6 +57,52 @@ func TestFilShort(t *testing.T) {
{fil: "0.000221234", expect: "221.234 μFIL"}, {fil: "0.000221234", expect: "221.234 μFIL"},
{fil: "0.0002212344", expect: "221.234 μFIL"}, {fil: "0.0002212344", expect: "221.234 μFIL"},
{fil: "0.00022123444", 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 s := s
t.Run(s.fil, func(t *testing.T) { t.Run(s.fil, func(t *testing.T) {