Class: Money::Allocation
- Inherits:
-
Object
- Object
- Money::Allocation
- Defined in:
- lib/money/money/allocation.rb
Class Method Summary collapse
-
.generate(amount, parts, whole_amounts = true) ⇒ Object
Splits a given amount in parts without loosing pennies.
Class Method Details
.generate(amount, parts, whole_amounts = true) ⇒ Object
Splits a given amount in parts without loosing pennies. The left-over 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.
The results should always add up to the original amount.
The parts can be specified as:
Numeric
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/money/money/allocation.rb', line 15 def self.generate(amount, parts, whole_amounts = true) parts = parts.is_a?(Numeric) ? Array.new(parts, 1) : parts.dup raise ArgumentError, 'need at least one party' if parts.empty? result = [] remaining_amount = amount until parts.empty? do parts_sum = parts.inject(0, :+) part = parts.pop current_split = remaining_amount * part / parts_sum current_split = current_split.truncate if whole_amounts result.unshift current_split remaining_amount -= current_split end result end |