Skip to content

Commit

Permalink
fix negative split remainder (#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Raymond <[email protected]>
  • Loading branch information
ConradKurth and Rhymond authored Mar 30, 2022
1 parent f3f6e05 commit 940609b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,15 @@ func (m *Money) Split(n int) ([]*Money, error) {
ms[i] = &Money{amount: a, currency: m.currency}
}

l := mutate.calc.modulus(m.amount, int64(n)).val

r := mutate.calc.modulus(m.amount, int64(n))
l := mutate.calc.absolute(r).val
// Add leftovers to the first parties.
for p := 0; l != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{1})
v := int64(1)
if a.val < 0 {
v = -1
}
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{v})
l--
}

Expand Down
1 change: 1 addition & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ func TestMoney_Split(t *testing.T) {
{100, 3, []int64{34, 33, 33}},
{100, 4, []int64{25, 25, 25, 25}},
{5, 3, []int64{2, 2, 1}},
{-101, 4, []int64{-26, -25, -25, -25}},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 940609b

Please sign in to comment.