0

In php I called round() function twice below.

1st call gets $ppp parameter, which is 5.585 when printed.

2nd call gets 5.585 as parameter as hard coded.

Why does round function returns 5.59 for the first one and 5.58 for the second one ?

$price = 75.3975; 
$ppp = $price - ($price/(1.08));
echo $ppp.'<br>'; //Prints 5.585
echo round($ppp,2,PHP_ROUND_HALF_EVEN).'<br>'; //Prints 5.59
echo round(5.585,2,PHP_ROUND_HALF_EVEN); //Prints 5.58
HOY
  • 1,067
  • 10
  • 42
  • 85
  • This seems a lot like the typical 0.1 + 0.2 != 0.3 issue people face. The *exact* value of `$ppp` might not be exactly `5.585` when calculated in floating-point. – Thomas Jager Sep 15 '21 at 17:06
  • Just another floating point problem. If you adjust the precision you'll see `$ppp` is slightly above 5.585 and the literal is slightly below. https://3v4l.org/4ecED – Don't Panic Sep 15 '21 at 17:07

0 Answers0