在MySQL中舍入浮点数是什么问题?

时间:2018-09-25 14:58:58

标签: mysql floating-point rounding

实际上,我们希望将数据库中的float转换为十进制。当我们检查这些值时,如果一切正确,我们发现mysql round有一个奇怪的行为,我们不知道为什么。

Table name: test
column name: test
column type: float

SQL: SELECT ROUND(test, 2) FROM test

-----------------------------------------
|    test    |  result of round(test,2) |
-----------------------------------------
|   12.225   |           12.23          |
-----------------------------------------
|   12.125   |           12.12          |
-----------------------------------------

1 个答案:

答案 0 :(得分:1)

请参阅documentation中的说明。

对于转化,使用CAST可能会获得更好的结果:

SELECT CAST(test as decimal(10,2)) FROM test