php中intval(0.58*100) 为啥输出 57\n?

【php中intval(0.58*100) 为啥输出 57\n?】 浮点数在计算机里面的计算是不精确的,本身是二进制的问题,二进制永远不能精确的表示出一个十进制的小数出来,具体原因你可以去百度找找资料,你可以看看十进制转二进制是怎么转的,浮点数是怎么转的,计算机里浮点数是用不精确的二进制计算之后再转成十进制给你看,有误差的

■网友
PHP 的 Manual 里有写,是 intval() 这个函数的问题,采用的是“截断”法取整。其实如果你直接输出 0.57*100 结果是正确的。intval converts doubles to integers by truncating the fractional component of the number. When dealing with some values, this can give odd results. Consider the following: print intval ((0.1 + 0.7) * 10); This will most likely print out 7, instead of the expected value of 8. For more information, see the section on floating point numbers in the PHP manual (http://www.php.net/manual/language.types.double.php) 解决方法:intval(strval(0.57*100)); // 57
■网友
这个问题我也被坑过,改成intval(0.58*10000)/100试试
■网友
php中intval(0.58*100) 为啥输出 57\n?

浮点数运算都会这样。
■网友
浮点数溢出了

■网友
先转为str型,再转回int型。试试(int)(string)($data*100)
■网友
consider intval(round(0.58*100))
■网友
和浮点数的特点有关系,浮点数的存储不是我们平时见到的样子,0.58,存起来依旧有一堆后缀,建议找一本计算机体系结构看看


    推荐阅读