Skip to content

Commit

Permalink
uint64 is too small
Browse files Browse the repository at this point in the history
  • Loading branch information
bibibong committed Jan 8, 2021
1 parent 20b7e64 commit ab13df8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions chain/types/epk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ func (f EPK) 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 EPK) 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)))
Expand Down
7 changes: 7 additions & 0 deletions chain/types/epk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ func TestEpkShort(t *testing.T) {
}{
{epk: "1", expect: "1 EPK"},
{epk: "1.1", expect: "1.1 EPK"},
{epk: "12", expect: "12 EPK"},
{epk: "123", expect: "123 EPK"},
{epk: "123456", expect: "123456 EPK"},
{epk: "123.23", expect: "123.23 EPK"},
{epk: "123456.234", expect: "123456.234 EPK"},
{epk: "123456.2341234", expect: "123456.234 EPK"},
{epk: "123456.234123445", expect: "123456.234 EPK"},

{epk: "0.1", expect: "100 mEPK"},
{epk: "0.01", expect: "10 mEPK"},
Expand Down

0 comments on commit ab13df8

Please sign in to comment.