module
Money::Allocate
Direct including types
Defined in:
money/money/allocate.crInstance Method Summary
-
#allocate(parts : Enumerable(Number)) : Array(Money)
Splits a given amount in parts without losing pennies.
-
#allocate(*parts : Number) : Array(Money)
Splits a given amount in parts without losing pennies.
-
#split(parts : Int) : Array(Money)
Splits money evenly amongst parties without losing pennies.
Instance Method Detail
Splits a given amount in parts without losing pennies.
The left-over pennies will be distributed round-robin amongst the parties. This means that parts listed first will likely receive more pennies than ones listed later.
Pass {2, 1, 1}
as input to give twice as much to part1 as part2 or part3
which results in 50% of the cash assigned to part1, 25% to part2,
and 25% to part3.
The results should always add up to the original amount.
Money.new(5, "USD").allocate(3, 7).map(&.cents) # => [2, 3]
Money.new(100, "USD").allocate(1, 1, 1).map(&.cents) # => [34, 33, 33]
Splits a given amount in parts without losing pennies.
The left-over pennies will be distributed round-robin amongst the parties. This means that parts listed first will likely receive more pennies than ones listed later.
Pass {2, 1, 1}
as input to give twice as much to part1 as part2 or part3
which results in 50% of the cash assigned to part1, 25% to part2,
and 25% to part3.
The results should always add up to the original amount.
Money.new(5, "USD").allocate(3, 7).map(&.cents) # => [2, 3]
Money.new(100, "USD").allocate(1, 1, 1).map(&.cents) # => [34, 33, 33]
Splits money evenly amongst parties without losing pennies.
Money.new(100, "USD").split(2).map(&.cents) # => [50, 50]
Money.new(100, "USD").split(3).map(&.cents) # => [34, 33, 33]