brick / money

A money and currency library for PHP
MIT License
1.61k stars 96 forks source link

Allocate method not working as expected. #73

Closed saffabook closed 1 year ago

saffabook commented 1 year ago

Am I misunderstanding the allocate function?

I have a total of 100000 I want to allocate this total and split it to the below percentages (which add to 100%); $divided = $currentTotal->allocate(8.23, 91.77);

This gives me the values of [8081, 91919]

But 8.23% of 100000 should be 8230 not 8081

Is there some error that I am doing?

BenMorel commented 1 year ago

Hi, allocate() uses integers as ratios:

public function allocate(int ...$ratios) : array

https://github.com/brick/money/blob/0.7.0/src/Money.php#L513

If you pass integers instead of floats, you get the correct result:

$divided = $currentTotal->allocate(823, 9177); // [8230.00, 91770.00]

I would advise to use strict_types to avoid PHP making a lossy float-to-int conversion in this case.

saffabook commented 1 year ago

Thank you.