Skip to content

Commit

Permalink
Merge pull request #12 from laojianzi/fix/to-fixed-decimal
Browse files Browse the repository at this point in the history
fix: ToFixed fractional part discard problem
  • Loading branch information
miraclesu authored Aug 24, 2021
2 parents 91a5f16 + 8761364 commit 228c69e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
3 changes: 1 addition & 2 deletions entities/fraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func (f *Fraction) ToFixed(decimalPlaces uint, opt ...number.Option) string {
f.opts.Apply(opt...)
f.opts.Apply(number.WithDecimalPlaces(decimalPlaces))

d := decimal.NewFromBigInt(big.NewInt(0).Div(f.Numerator, f.Denominator), 0)

d := decimal.NewFromBigInt(f.Numerator, 0).Div(decimal.NewFromBigInt(f.Denominator, 0))
return number.DecimalFormat(d, f.opts)
}
74 changes: 74 additions & 0 deletions entities/percent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package entities

import (
"math/big"
"testing"

"github.com/miraclesu/uniswap-sdk-go/number"
)

// nolint:dupl
func TestPercent_ToSignificant(t *testing.T) {
type fields struct {
num, deno *big.Int
}
type args struct {
significantDigits uint
opt []number.Option
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
"returns the value scaled by 100",
fields{big.NewInt(154), big.NewInt(10000)},
args{significantDigits: 3},
"1.54",
},
}
for _, tt := range tests {
got := NewPercent(tt.fields.num, tt.fields.deno).ToSignificant(tt.args.significantDigits, tt.args.opt...)
want := tt.want
t.Run(tt.name, func(t *testing.T) {
if got != want {
t.Errorf("ToSignificant() = %v, want %v", got, want)
}
})
}
}

// nolint: dupl
func TestPercent_ToFixed(t *testing.T) {
type fields struct {
num, deno *big.Int
}
type args struct {
decimalPlaces uint
opt []number.Option
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
"returns the value scaled by 100",
fields{big.NewInt(154), big.NewInt(10000)},
args{decimalPlaces: 2},
"1.54",
},
}
for _, tt := range tests {
got := NewPercent(tt.fields.num, tt.fields.deno).ToFixed(tt.args.decimalPlaces, tt.args.opt...)
want := tt.want
t.Run(tt.name, func(t *testing.T) {
if got != want {
t.Errorf("ToFixed() = %v, want %v", got, want)
}
})
}
}

0 comments on commit 228c69e

Please sign in to comment.