brick / money

A money and currency library for PHP
MIT License
1.61k stars 96 forks source link

Feature: implement splitWithRemainder() & allocateWithRemainder() #39

Closed BenMorel closed 3 years ago

BenMorel commented 3 years ago

Same as split() and allocate(), but:

Taking the examples from the README:

$money = Money::of(100, 'USD');
[$a, $b, $c, $r] = $money->splitWithRemainder(3); // USD 33.33, USD 33.33, USD 33.33, USD 0.01
$profit = Money::of('987.65', 'CHF');
[$a, $b, $c, $r] = $profit->allocateWithRemainder(48, 41, 11); // CHF 474.08, CHF 404.93, CHF 108.64, CHF 0.01
$profit = Money::of('987.65', 'CHF', new CashContext(5));
[$a, $b, $c, $r] = $profit->allocateWithRemainder(48, 41, 11); // CHF 474.05, CHF 404.90, CHF 108.60, CHF 0.10
NCatalani commented 3 years ago

Does the remainder needs to be appended at the end even on exact divisions?

BenMorel commented 3 years ago

@NCatalani I think so, yes. I'd rather get a 0 USD remainder than having to check for existence of the nth value in the returned array.