brick / money

A money and currency library for PHP
MIT License
1.65k stars 102 forks source link

Issue with documentation of allocate() #45

Closed nammet closed 2 years ago

nammet commented 3 years ago

The readme says the following:

When the allocation yields a remainder, both split() and allocate() spread it on the first monies in the list, until the total adds up to the original Money. This is the algorithm suggested by Martin Fowler in his book Patterns of Enterprise Application Architecture. You can see that in the first example, where the first money gets 33.34 dollars while the others get 33.33 dollars.

However it looks to me that split() spreads in the way described, but allocate appears to use the ratios to divide up the remainder:



$profit = Money::ofMinor('1000', 'GBP');
[$a, $b, $c] = $profit->allocate(45, 20, 34); // GBP 45.50, GBP 20.20, GBP 34.30
BenMorel commented 3 years ago

Hi, the output of your script above is not the actual one, I'm not sure where you got it from. The actual output is:

GBP 4.55
GBP 2.02
GBP 3.43

Which is the expected result:

Remainder = 0.01, which is added to the first result so 4.55.

BenMorel commented 2 years ago

Closing due to lack of feedback.