-2

The following operation give out the wrong result.

$First      = '45.4000';
$Second     = '4.6800000000';
$Third      = '50.00';

echo ( $First + $Second ) - $Third;

OUTPUT: 0.079999999999998

Expected Output: 0.08

I am looking on how to get the right result, without using number_format/sprintf ...etc.

As this issue is affecting multiple places in my code & have to go over everything & formatting it is a pain.

Tareq
  • 92
  • 4

2 Answers2

-1

As a "quick fix", change the precision setting in your php.ini file. Documentation.

By default, it is 14, which is more than you need almost all the time (and if you need that much precision you'd be using a dedicated math library). Change it to something like 4, and the result will be rounded to that length - note that you can still override this with number_format on a case-by-case basis if you need to.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
-1

Try This

$First      = '45.4000';
$Second     = '4.6800000000';
$Third      = '50.00';

$sk = ( $First + $Second ) - $Third;

echo round($sk,4);

?>
Dev
  • 489
  • 5
  • 14