BCMath - imitate calculator or excel

18 views Asked by At

I'm working with decimal values that can have more decimal digits than the float type, so I have to use bcmath functions. My problem is that standard calculators like in Windows, Android or PHP operators can calculate terms like a / b * b = a

Is there a way to get the same result with BCMath (as the floating point version) but don't lose precision if needed?

$a = '897.8';
$b = '1046.15';

$c = bcdiv($a, $b, 15);

$d = bcmul($c, $b, 15);

print "C:".$c."\n";
print "D:".$d."\n";

print 897.8 / 1046.15 * 1046.15. "\n";

$e = 897.8 / 1046.15;
print $e. "\n";
print $e * 1046.15;

Output:

C:0.858194331596807
D:897.799999999999643
897.8
0.85819433159681
897.8
0

There are 0 answers