uint64 is too small

This commit is contained in:
Łukasz Magiera 2020-12-01 19:15:39 +01:00
parent 15c4793fe0
commit be9f7b88b2
2 changed files with 14 additions and 4 deletions

View File

@ -23,18 +23,19 @@ func (f FIL) Unitless() string {
return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".")
}
var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m", ""}
var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m"}
func (f FIL) Short() string {
n := BigInt(f)
dn := uint64(1)
var prefix string
for _, prefix = range unitPrefixes {
for _, p := range unitPrefixes {
if n.LessThan(NewInt(dn * 1000)) {
prefix = p
break
}
dn = dn * 1000
dn *= 1000
}
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(dn)))

View File

@ -1,8 +1,9 @@
package types
import (
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/require"
)
func TestFilShort(t *testing.T) {
@ -10,8 +11,16 @@ func TestFilShort(t *testing.T) {
fil string
expect string
}{
{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"},