Floor () in php not working

<?php
    echo gettype ( 5.00 );                                  // return double
    echo gettype((5));                                      // return integer
    echo gettype(((167.00-158.65)/167.00*100));             // return double

    echo floor(5.00);                                       // return 5
    echo floor(5);                                          // return 5
    echo ((167.00-158.65)/167.00*100);                      // return 5
    echo floor(((167.00-158.65)/167.00*100));               // return 4



    var_dump(5.00);                                         // return float(5)
    var_dump(5);                                            // return int(5)
    var_dump((167.00-158.65)/167.00*100);                   // return float(5)
    var_dump(intval(5));                                    // return int(5)
    var_dump(intval((167.00-158.65)/167.00*100));           // return int(4)

    echo gettype(intval(((167.00-158.65)/167.00*100)));     // return integer
    echo floor(intval((167.00-158.65)/167.00*100));         // return 4
?>

Why does the floor function in php not work in the latter case?

How to get 5from the last statement? Is there any other function or method in php to get the exact least amount?

+4
source share
2 answers

This behavior is caused by the limited precision of floating point numbers. The last case is of type float(check it with var_dump), and the manual says:

A warning

. , PHP, , IEEE 754, - 1.11-16. , , , , .

, , 10, 0,1 0,7, 2, , . , . : , ((0,1 + 0,7) * 10) 7 8, - 7,9999999999999991118....

:

http://php.net/manual/en/language.types.float.php

+3

floor() . floor() - float, float , . FALSE (, ).

<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>
+2

Source: https://habr.com/ru/post/1624789/


All Articles