module Money::Allocate

Direct including types

Defined in:

money/money/allocate.cr

Instance Method Summary

Instance Method Detail

def allocate(splits : Enumerable(Number)) : Array(Money) #

Allocates money between different parties without losing pennies. After the mathematical split has been performed, leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later.

# Give 50% of the cash to party 1, 25% to party 2, and 25% to party 3.
Money.new(10_00, "USD").allocate([0.5, 0.25, 0.25]).map(&.cents)
# => [5_00, 2_50, 2_50]

Money.new(5, "USD").allocate({0.3, 0.7}).map(&.cents)
# => [2, 3]

Money.new(100, "USD").allocate(0.33, 0.33, 0.33).map(&.cents)
# => [34, 33, 33]

[View source]
def allocate(*splits : Number) : Array(Money) #

Allocates money between different parties without losing pennies. After the mathematical split has been performed, leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later.

# Give 50% of the cash to party 1, 25% to party 2, and 25% to party 3.
Money.new(10_00, "USD").allocate([0.5, 0.25, 0.25]).map(&.cents)
# => [5_00, 2_50, 2_50]

Money.new(5, "USD").allocate({0.3, 0.7}).map(&.cents)
# => [2, 3]

Money.new(100, "USD").allocate(0.33, 0.33, 0.33).map(&.cents)
# => [34, 33, 33]

[View source]
def split(num : Int) : Array(Money) #

Splits money amongst parties evenly without losing pennies.

Money.new(100, "USD").split(3).map(&.cents) # => [34, 33, 33]

[View source]