Skip to content

Commit da9a2f7

Browse files
authored
Merge pull request #14 from jon4hz/main
Fix ToSignificant() output on very small numbers
2 parents 228c69e + c4cca6f commit da9a2f7

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

entities/fraction.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package entities
22

33
import (
44
"math/big"
5+
"strings"
56

67
"github.com/shopspring/decimal"
78

89
"github.com/miraclesu/uniswap-sdk-go/constants"
910
"github.com/miraclesu/uniswap-sdk-go/number"
1011
)
1112

12-
var (
13-
// ZeroFraction zero fraction instance
14-
ZeroFraction = NewFraction(constants.Zero, nil)
15-
)
13+
// ZeroFraction zero fraction instance
14+
var ZeroFraction = NewFraction(constants.Zero, nil)
15+
16+
const decimalSplitLength = 2
1617

1718
// Fraction warps math franction
1819
type Fraction struct {
@@ -120,16 +121,31 @@ func (f *Fraction) Divide(other *Fraction) *Fraction {
120121
func (f *Fraction) ToSignificant(significantDigits uint, opt ...number.Option) string {
121122
f.opts = number.New(number.WithGroupSeparator('\xA0'), number.WithRoundingMode(constants.RoundHalfUp))
122123
f.opts.Apply(opt...)
123-
f.opts.Apply(number.WithRoundingPrecision(int(significantDigits)))
124124

125125
d := decimal.NewFromBigInt(f.Numerator, 0).Div(decimal.NewFromBigInt(f.Denominator, 0))
126+
if d.LessThan(decimal.New(1, 0)) {
127+
significantDigits += countZerosAfterDecimalPoint(d.String())
128+
}
129+
f.opts.Apply(number.WithRoundingPrecision(int(significantDigits)))
126130
if v, err := number.DecimalRound(d, f.opts); err == nil {
127131
d = v
128132
}
129-
130133
return number.DecimalFormat(d, f.opts)
131134
}
132135

136+
func countZerosAfterDecimalPoint(d string) uint {
137+
grp := strings.Split(d, ".")
138+
if len(grp) != decimalSplitLength {
139+
return 0
140+
}
141+
for i, v := range grp[1] {
142+
if v != '0' {
143+
return uint(i)
144+
}
145+
}
146+
return 0
147+
}
148+
133149
// ToFixed format output
134150
func (f *Fraction) ToFixed(decimalPlaces uint, opt ...number.Option) string {
135151
f.opts = number.New(number.WithGroupSeparator('\xA0'), number.WithRoundingMode(constants.RoundHalfUp))

entities/fraction_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestQuotient(t *testing.T) {
9-
var tests = []struct {
9+
tests := []struct {
1010
Input [2]int64
1111
Output int64
1212
}{
@@ -23,7 +23,7 @@ func TestQuotient(t *testing.T) {
2323
}
2424

2525
func TestRemainder(t *testing.T) {
26-
var tests = []struct {
26+
tests := []struct {
2727
Input [2]int64
2828
Output [2]int64
2929
}{
@@ -41,7 +41,7 @@ func TestRemainder(t *testing.T) {
4141
}
4242

4343
func TestInvert(t *testing.T) {
44-
var tests = []struct {
44+
tests := []struct {
4545
Input [2]int64
4646
Output [2]int64
4747
}{
@@ -56,7 +56,7 @@ func TestInvert(t *testing.T) {
5656
}
5757

5858
func TestAdd(t *testing.T) {
59-
var tests = []struct {
59+
tests := []struct {
6060
Input [4]int64
6161
Output [2]int64
6262
}{
@@ -75,7 +75,7 @@ func TestAdd(t *testing.T) {
7575
}
7676

7777
func TestSubtract(t *testing.T) {
78-
var tests = []struct {
78+
tests := []struct {
7979
Input [4]int64
8080
Output [2]int64
8181
}{
@@ -94,7 +94,7 @@ func TestSubtract(t *testing.T) {
9494
}
9595

9696
func TestLessThan(t *testing.T) {
97-
var tests = []struct {
97+
tests := []struct {
9898
Input [4]int64
9999
Output bool
100100
}{
@@ -112,7 +112,7 @@ func TestLessThan(t *testing.T) {
112112
}
113113

114114
func TestEqualTo(t *testing.T) {
115-
var tests = []struct {
115+
tests := []struct {
116116
Input [4]int64
117117
Output bool
118118
}{
@@ -131,7 +131,7 @@ func TestEqualTo(t *testing.T) {
131131
}
132132

133133
func TestGreaterThan(t *testing.T) {
134-
var tests = []struct {
134+
tests := []struct {
135135
Input [4]int64
136136
Output bool
137137
}{
@@ -151,7 +151,7 @@ func TestGreaterThan(t *testing.T) {
151151
}
152152

153153
func TestMultiply(t *testing.T) {
154-
var tests = []struct {
154+
tests := []struct {
155155
Input [4]int64
156156
Output [2]int64
157157
}{
@@ -171,7 +171,7 @@ func TestMultiply(t *testing.T) {
171171
}
172172

173173
func TestDivide(t *testing.T) {
174-
var tests = []struct {
174+
tests := []struct {
175175
Input [4]int64
176176
Output [2]int64
177177
}{
@@ -192,7 +192,7 @@ func TestDivide(t *testing.T) {
192192
}
193193

194194
func TestToSignificant(t *testing.T) {
195-
var tests = []struct {
195+
tests := []struct {
196196
Input [2]int64
197197
Output string
198198
Format uint
@@ -203,6 +203,7 @@ func TestToSignificant(t *testing.T) {
203203
{[2]int64{126, 100}, "1.26", 2},
204204
{[2]int64{124, 100}, "1.2", 1},
205205
{[2]int64{124, 100}, "1.24", 2},
206+
{[2]int64{125, 1000000000}, "0.00000013", 2},
206207
}
207208
for i, test := range tests {
208209
output := NewFraction(big.NewInt(test.Input[0]), big.NewInt(test.Input[1])).ToSignificant(test.Format)

0 commit comments

Comments
 (0)