diff --git a/bill/payment.go b/bill/payment.go index abbbd771..e643b91d 100644 --- a/bill/payment.go +++ b/bill/payment.go @@ -53,6 +53,7 @@ func (p *Payment) totalAdvance(zero num.Amount) *num.Amount { } sum := zero for _, a := range p.Advances { + sum = sum.MatchPrecision(a.Amount) sum = sum.Add(a.Amount) a.Amount = a.Amount.Rescale(zero.Exp()) } diff --git a/bill/payment_test.go b/bill/payment_test.go index 61d340cc..7e170e93 100644 --- a/bill/payment_test.go +++ b/bill/payment_test.go @@ -51,4 +51,22 @@ func TestPaymentCalculations(t *testing.T) { p.calculateAdvances(zero, total) sum := p.totalAdvance(zero) assert.Equal(t, "30.00", sum.String()) + + t.Run("maintains precision", func(t *testing.T) { + zero := num.MakeAmount(0, 2) + total := num.MakeAmount(20845, 3) + p := &Payment{ + Advances: []*pay.Advance{ + { + Description: "Paid in advance", + Percent: num.NewPercentage(100, 2), + }, + }, + } + p.calculateAdvances(zero, total) + a := p.totalAdvance(zero) + + assert.Equal(t, "20.85", p.Advances[0].Amount.String()) + assert.Equal(t, "20.845", a.String()) + }) }