签名:0b0有偏指数:0b01111111= 127(表示包括 +127 的偏差,因此 exp = 0)有效数 : 0b[1.]11001100110011001100110^ 隐藏位,不以二进制表示形式存储= [1.]7999999523162841796875这相当于:(-1)^0 * 1.7999999523162841796875 * 2^0= 1.7999999523162841796875这是 MySQL 返回给客户端的值.看起来 AdoDB 然后检查了列的数据类型并相应地对结果进行了四舍五入,而 PDO 则没有.如果你想要精确的值,你应该使用固定点数据类型,例如 DECIMAL.I am guessing this has came up before, but I couldn't find the answer to my question. Here is a little code snippet: $stmt = $this -> db -> query(" SELECT `Field` FROM `Table` WHERE (`ID` = 33608)"); var_dump($stmt -> fetch());And this is the result I get: array(1) { ["Field"]=> float(1.7999999523163) }However, the data in the MySQL database is 1.8. The type of the field is float(7,4). $this->db is a PDO object.I have recently migrated to PDO (from AdoDB), and this code was working fine before. I am not sure what went wrong here. Could you point me in the right direction?Thanks! 解决方案 As documented under Floating-Point Types (Approximate Value) - FLOAT, DOUBLE:Therefore, upon inserting 1.8 into your database, MySQL rounded the literal to 001.8000 and encoded the closest approximation to that number in binary32 format: i.e. 0x3FE66666, whose bits signify:Sign : 0b0Biased exponent: 0b01111111 = 127 (representation includes bias of +127, therefore exp = 0)Significand : 0b[1.]11001100110011001100110 ^ hidden bit, not stored in binary representation = [1.]7999999523162841796875This equates to: (-1)^0 * 1.7999999523162841796875 * 2^0= 1.7999999523162841796875This is the value that is returned by MySQL to the client. It would appear that AdoDB then inspected the column's datatype and rounded the result accordingly, whereas PDO does not.If you want exact values, you should use a fixed point datatype, such as DECIMAL. 这篇关于PHP PDO 查询为 FLOAT 字段返回不准确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!