Configure rounding strategies for allocation
mainWhen using Money::Allocator#allocate, you can specify a rounding strategy to determine how leftover subunits are distributed. Strategies include:
:roundrobin: Assigns leftover subunits from left to right.:roundrobin_reverse: Assigns leftover subunits from right to left.:nearest: Assigns leftover subunits to the nearest whole subunit.
You can set a global default via Money.configure or pass the strategy as an argument to the allocate method.
# Global default
Money.configure do |config|
config.default_allocation_strategy = :nearest
end
# Per-call strategy
m = Money.new(10.55, "USD")
m.allocate([0.25, 0.5, 0.25], :nearest)# Assigns leftover subunits left to right
m = Money::Allocator.new(Money.new(10.55, "USD"))
monies = m.allocate([0.25, 0.5, 0.25], :roundrobin)
# Assigns leftover subunits right to left
m = Money::Allocator.new(Money.new(10.55, "USD"))
monies = m.allocate([0.25, 0.5, 0.25], :roundrobin_reverse)
# Assigns leftover subunits to the nearest whole subunit
m = Money::Allocator.new(Money.new(10.55, "USD"))
monies = m.allocate([0.25, 0.5, 0.25], :nearest)