0

Need to understand this error and resolve it:

<?php
$a = 204.95 + 204.95 + 204.95;
$b = 614.85;
echo $a ."->" .$b ."->" . ($b - $a)."\n";


$a = 204.93 + 204.93 + 204.93;
$b = 614.79;


echo $a ."->" .$b ."->" . ($b - $a)."\n";
?>

Getting output:

614.85->614.85->1.1368683772162E-13
614.79->614.79->0

Expected Result is:

614.85->614.85->0
614.79->614.79->0
IAmMilinPatel
  • 413
  • 1
  • 11
  • 19
  • The solution in "Is floating point math broken?" is to use abs(). But in my case I cannot. As this is related to price calculation, the decimal values are really important. I cannot round off or use absolute value. – IAmMilinPatel Dec 30 '22 at 05:12
  • 2
    IAmMilinPatel: The actual value in your code is https://3v4l.org/11jqT. You need to use `number_format()` with `2` decimal precision: https://3v4l.org/qPscr. Otherwise, you will always get some sort of miscalculation. – Alive to die - Anant Dec 30 '22 at 05:25

0 Answers0