Functional Weave
Code in Rust

money.split-even@1.0.0

README.md

1,052 bytes · view raw

# money.split-even

Splitting 10.00 three ways gives 3.34, 3.33, 3.33: every part is the whole
divided by n, rounded down, and the minor units left over go one each to the
first parts. The parts always add back up to the amount, never differ by more
than one minor unit, and the larger ones come first, so the first instalment
or the first diner absorbs the odd penny.

Negative amounts (refunds, credit notes) are split as the mirror image of the
positive split: -10.00 three ways is -3.34, -3.33, -3.33. That is where this
differs from `money.allocate` with equal ratios, which rounds toward negative
infinity and so puts the odd penny on the last part of a negative amount
(-3.33, -3.33, -3.34). Refunding a split bill with this capability undoes the
original split part for part.

It does not build on `money.allocate` for that reason, and because an even
split needs no ratios: it is one division and one remainder.

The minor unit is whatever the currency's is; 100 yen three ways is 34, 33, 33
yen. `parts` must be a whole number of at least 1.