Skip to content

Core\Decimal::allocate

Splits an amount into parts that add back to it exactly.

You give the sum and one ratio for each part. Core\Decimal::allocate returns one part per ratio, at the same scale as the amount. A price written with two digits after the point is split into whole cents. The parts always add back up to the amount, so no money is lost and none is invented.

Doing this by hand goes wrong. Three equal parts of 0.05 are 0.0166… each. Rounded, that is 0.02 three times, which adds up to 0.06. This member gives each leftover unit to one of the earliest parts instead. The keys of the ratios become the keys of the result, so a split written under the names of the people it belongs to returns those names.

Good to know: position decides who gets a leftover unit, not weight. A ratio may not be negative, and at least one ratio must be above zero.

The examples below show a bill split evenly, a payout split by named shares, and a discount spread over the lines of an order.

Parameters

  • $amountdecimalThe sum to split. The parts have the same number of digits after the point, so a price with two digits splits into parts with two digits.
  • $ratiosarray<decimal>One weight per part. Each weight is zero or more, and at least one is above zero. The keys are kept, so you can name each part.

Return value

Returns array<decimal>. One part per ratio, in the same order and with the same keys. Each part is its share, rounded towards zero. The first parts then get one smallest unit more each, until nothing is left over.

Errors

  • LogicErrorWhen there are no ratios, when a ratio is negative, or when every ratio is zero. In each case there is no part to make.
  • ArithmeticErrorWhen a part does not fit in a decimal. That happens when it needs more than 28 digits after the point, or more than a 96-bit mantissa.

Examples

Split a bill evenly

<?nvs
// Shows how to split a bill evenly between three people. The parts add back to
// the bill exactly, so the three payments always cover it.
decimal $bill = 20.05;
// One ratio per person. They are all the same, so the three parts are as close
// to equal as whole cents allow.
array<decimal> $evenly = [1, 1, 1];
decimal $paid = 0.00;
foreach (Core\Decimal::allocate($bill, $evenly) as decimal $part) {
echo $part, "\n";
$paid = $paid + $part;
}
// This prints 20.05. The cent that does not divide went to the first person.
echo 'total: ', $paid, "\n";
Output
6.69
6.68
6.68
total: 20.05

Split by named shares

<?nvs
// Shows how to split a payout between partners who own different shares. The
// keys of the ratios are the keys of the result, so each part comes back under
// the name it belongs to.
decimal $payout = 1000.00;
// Ada owns five parts of the business, Grace three and Alan two.
array<decimal> $shares = ['Ada' => 5, 'Grace' => 3, 'Alan' => 2];
foreach (Core\Decimal::allocate($payout, $shares) as string $name => decimal $part) {
echo $name, ' receives ', $part, "\n";
}
Output
Ada receives 500.00
Grace receives 300.00
Alan receives 200.00

Spread a discount over an order

<?nvs
// Shows how to spread a discount over the lines of an order. Each line takes a
// share of the discount in proportion to its price, and the shares add back to
// the discount exactly.
array<decimal> $prices = [19.99, 5.00, 34.50];
decimal $discount = 10.00;
// The prices are the ratios, so a line that costs more takes more of the cut.
array<decimal> $cuts = Core\Decimal::allocate($discount, $prices);
decimal $given = 0.00;
uint $line = 0;
foreach ($prices as decimal $price) {
decimal $cut = $cuts[$line];
echo $price, ' - ', $cut, ' = ', $price - $cut, "\n";
$given = $given + $cut;
$line = $line + 1;
}
// This prints 10.00. The whole discount was given out, to the cent.
echo 'discount given: ', $given, "\n";
Output
19.99 - 3.37 = 16.62
5.00 - 0.84 = 4.16
34.50 - 5.79 = 28.71
discount given: 10.00