0

I am getting a wrong result when I perform the following operation

$n1 = 178.200;
$n2 = 89.100;
$n3 = 178.199;
$n4 = 89.101;
$n5 = 170.999;
$grandTotal = $n1+$n2+$n3+$n4+$n5;
$discount = 700.000;
$netTotal = $grandTotal - $discount;
echo "Grand Total:".$grandTotal."<br>";
echo "Discount:".$discount."<br>";
echo "Net Total:".$netTotal."<br>";

Output:

Grand Total: 705.599
Discount: 700.000
Net Total: 5.5989999999999

But 705.599-700.000 = 5.599 right ?

Why Im getting this 5.5989999999999 ?

Qirel
  • 25,449
  • 7
  • 45
  • 62
VIPIN A ROY
  • 1,761
  • 5
  • 28
  • 41
  • [What Every Programmer Should Know AboutFloating-Point Arithmetic](http://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf) – RiggsFolly May 15 '19 at 11:39
  • You are probably looking for [bcmath extension](https://www.php.net/manual/en/book.bc.php) `bcscale(3); $grandTotal = bcadd($n1, $n2); $grandTotal = bcadd($grandTotal, $n3); $grandTotal = bcadd($grandTotal, $n4); $grandTotal = bcadd($grandTotal, $n5); $netTotal = bcsub($grandTotal, $discount);` – akaincore May 15 '19 at 11:49

0 Answers0