Skip to content

Commit

Permalink
Merge pull request #4 from miraclesu/fix/decimal-round
Browse files Browse the repository at this point in the history
fix: fraction decimal rounding
  • Loading branch information
miraclesu authored Jun 26, 2021
2 parents 0e0db36 + 4df8154 commit 0c048f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion entities/fraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (f *Fraction) Divide(other *Fraction) *Fraction {
func (f *Fraction) ToSignificant(significantDigits uint, opt ...number.Option) string {
f.opts = number.New(number.WithGroupSeparator('\xA0'), number.WithRoundingMode(constants.RoundHalfUp))
f.opts.Apply(opt...)
f.opts.Apply(number.WithRoundingPrecision(int(significantDigits) + 1))
f.opts.Apply(number.WithRoundingPrecision(int(significantDigits)))

d := decimal.NewFromBigInt(f.Numerator, 0).Div(decimal.NewFromBigInt(f.Denominator, 0))
if v, err := number.DecimalRound(d, f.opts); err == nil {
Expand Down
8 changes: 5 additions & 3 deletions entities/fraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ func TestToSignificant(t *testing.T) {
Format uint
}{
{[2]int64{30, 10}, "3", 0},
{[2]int64{4, 10}, "0.4", 0},
{[2]int64{126, 100}, "1.26", 1},
{[2]int64{1, 1000}, "0.001", 2},
{[2]int64{4, 10}, "0.4", 1},
{[2]int64{126, 100}, "1.3", 1},
{[2]int64{126, 100}, "1.26", 2},
{[2]int64{124, 100}, "1.2", 1},
{[2]int64{124, 100}, "1.24", 2},
}
for i, test := range tests {
output := NewFraction(big.NewInt(test.Input[0]), big.NewInt(test.Input[1])).ToSignificant(test.Format)
Expand Down
18 changes: 10 additions & 8 deletions number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,10 @@ func TestDecimalFormat(t *testing.T) {
},
}

for _, tt := range tests {
for i, tt := range tests {
if got := DecimalFormat(tt.args.d, tt.args.opts); got != tt.want {
t.Errorf("DecimalFormat() = %v, want %v", got, tt.want)
t.Errorf("DecimalFormat([%d]{d:[%+v], opts:[%+v]}) got = %v, want %v", i, tt.args.d.String(), tt.args.opts,
got, tt.want)
}
}
}
Expand All @@ -771,9 +772,9 @@ func TestDecimalRound(t *testing.T) {
{
args: args{
d: mustNewFromString("0.00001000"),
opts: New(WithRoundingPrecision(2), WithRoundingMode(constants.RoundUp)),
opts: New(WithRoundingPrecision(4), WithRoundingMode(constants.RoundUp)),
},
want: mustNewFromString("0.01"),
want: mustNewFromString("0.0001"),
},
{
args: args{
Expand Down Expand Up @@ -806,9 +807,9 @@ func TestDecimalRound(t *testing.T) {
{
args: args{
d: mustNewFromString("-0.05"),
opts: New(WithRoundingPrecision(0), WithRoundingMode(constants.RoundUp)),
opts: New(WithRoundingPrecision(1), WithRoundingMode(constants.RoundUp)),
},
want: mustNewFromString("-1"),
want: mustNewFromString("-0.1"),
},
{
args: args{
Expand Down Expand Up @@ -1735,14 +1736,15 @@ func TestDecimalRound(t *testing.T) {
want: mustNewFromString("2988347090930431122495200201632971168964831173901728"),
},
}
for _, tt := range tests {
for i, tt := range tests {
got, err := DecimalRound(tt.args.d, tt.args.opts)
if err != nil {
t.Fatal(err)
}

if got.String() != tt.want.String() {
t.Errorf("DecimalRound() got = %v, want %v", got.String(), tt.want.String())
t.Errorf("DecimalRound([%d]{d:[%+v], opts:[%+v]}) got = %v, want %v", i, tt.args.d.String(), tt.args.opts,
got.String(), tt.want.String())
}
}
}

0 comments on commit 0c048f9

Please sign in to comment.