Skip to content

Potential bug in money allocation #111

Closed

Description

@zenas1210

Hi,

#110

Intuitively I would expect the allocation to be 98, 2 and 120:

Image Image

Attaching the excel file from the screenshot.

Is this a bug or am I possibly misunderstanding the way this is supposed to work?

test case.xlsx

Activity

  1. BenMorel commented on Jan 31, 2026

    @BenMorel
    Member

    Hi, this is not a bug but by design. Quoting the README:

    When the allocation yields a remainder, both split() and allocate() spread it on the first monies in the list, until the total adds up to the original Money. This is the algorithm suggested by Martin Fowler in his book Patterns of Enterprise Application Architecture. You can see that in the first example, where the first money gets 33.34 dollars while the others get 33.33 dollars.

    In your example, the 545455 one gets < 120, so the allocator will never give it 120, even if it would make sense if we rounded everything.

    If you think we can add an API for this use case, feel free to open a PR!

  2. BenMorel commented on Mar 29, 2026

    @BenMorel
    Member

    New allocation modes are now available through AllocationMode in version 0.13.0, which might fit your use case:

    $money = Money::of('220', 'GBP');
    
    $money->allocate([445455, 9091, 545455], AllocationMode::FloorToFirst);            // 98.01, 2.00, 119.99
    $money->allocate([445455, 9091, 545455], AllocationMode::FloorToLargestRemainder); // 98.00, 2.00, 120.00
  3. zenas1210 commented on Mar 29, 2026

    @zenas1210
    Author

    Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions