moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.6k stars 440 forks source link

Mutliplier + Divisor - Why not a float? #650

Closed internalsystemerror closed 3 years ago

internalsystemerror commented 3 years ago

When using the Money::multiply and Money::divide methods, thanks to the changes in https://github.com/moneyphp/money/pull/648 we can pass an integer as well as a numeric string. It should also be possible to pass a float to these methods such as when calculating tax: $incTax = $money->multiply(1.2);.

If you agree then I can submit a PR to make the necessary adjustments.

frederikbosch commented 3 years ago

You would have to convert the float to string before passing it to multiply or divide. Converting floats to string has the risk of inaccuracy which of the risk of the caller. Run the following in your terminal.

$a = '35';
$b = '-34.99';
echo ($a + $b);

Now suppose you call $money->multiply($a + $b). A float passed is not to be trusted. From version 4.0 we force callers to use int and string only, doing the conversion themselves.

frederikbosch commented 3 years ago

I will leave this open for now because this not documented well enough in the CHANGELOG.

pmochine commented 2 years ago

@frederikbosch It would be cool if you would show this in the documentation. https://www.moneyphp.org/en/stable/features/operation.html#multiplication-division

Now after coming back to a project float is not working