Closed anhskohbo closed 4 years ago
1050 / 100 ~= 11, 11 * 20 = 220. Seems legit. Just swap multiplication and division to avoid the loss of precision in the first step.
I tried, the default rounding mode is rounded up at the first step divide(100)
.
<?php
$money = Money::USD('1050');
$onePercent = $money->divide(100); // amount value is 11.
Seem no way to correct that. I will let customers will be lost one cent in that case :)
You only need to multiply first:
1050 * 20 = 21000
21000 / 100 = 210
As @UlrichEckhardt said, you lose precision between the two steps, because it's not preserved between calculations. After dividing the initial value, you get back a money object, not an intermediary result.
You either have to switch the order of operations, or really just merge them:
Money::USD('1050')->multiply(20/100)
Hello, I can't make this work correctly when working with cents.
I have try to pass $roundingMode but still not correct.