PHP unexpected result of float to int type cast

前端 未结 4 1538
说谎
说谎 2020-11-29 08:00

I\'trying to convert a float to an int value in php:

var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930!
var_dump((int)(39.2 * 100.0)); //Retur         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-29 08:28

    // gives: int(3930)
    var_dump(intval((39.3 * 100.0) . '')); 
    

    or, for use with a function:

    function floatToInteger ($fValue)
    {
        return (intval(($fValue + 0) . ''));
    }
    
    // gives: int(3930)
    var_dump(floatToInteger(39.3 * 100.0));
    

提交回复
热议问题