1


yesterday I found some issue in my code, but I don't understand what wrong is with PHP Floor function. For some values Floor() is not rounding correctly integer values.

$prise = 550.05 * 100;
echo $prise;                    // 55005

echo "\nFloor: ";
echo floor(intval($prise));
echo " - ";
echo floor((int)($prise));
echo " - ";
echo floor($prise);
echo " - ";
echo floor(55005);              // Floor: 55004 - 55004 - 55004 - 55005

echo "\nNumber format: ";
echo number_format($prise, 0, '.', '');    // Number format: 55005

As you can see, correct value should equal 55005. For unknown reasons, the function still rounds the not decimal value. Could someone explain to me how it works, or is it a PHP issue?

Example in online PHP sandbox: http://sandbox.onlinephpfunctions.com/code/55b1782e1f3799815d610777a3de1c2d297e24c8

Thanks in advance

Daniel
  • 88
  • 1
  • 2
  • 10
  • There's no point in calling `floor()` after you've already converted the argument to an integer. – Barmar Jul 11 '19 at 06:58
  • 1
    `550.05` can't be represented exactly in floating point, the approximation is something like `550.0499999999`. Multiplying by 100 produces `55004.99999999`, so floor is `55004`. It's best to use `round()` instead of `floor()` so it doesn't matter whether the approximation is high or low. – Barmar Jul 11 '19 at 07:00

0 Answers0