I'm trying to calculate a tax or discount value using BCMATH in PHP. I need 2 DECIMAL PLACES. Here's how i'm doing it:
bcscale(2);
$price = '60.67';
$discount = bcmul(bcdiv($price, 100), '3.8'); // calculate 3.8 percent from 60.67
// result is: 2.28
// result should be: 2.31
I could simply increase the scale to 4 or something like that and that would now give me a correct result.
So the question is: how should i do this correctly? Should i set the scale to 4 and then sprintf('%0.2f', $discount) in the end? Or is there a better way of doing this?