mysql中舍入浮点有什么问题?

9jyewag0  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(299)

实际上,我们希望在数据库中从float切换到decimal。当我们检查值时,如果一切都是正确的,我们发现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          |
-----------------------------------------
yqkkidmi

yqkkidmi1#

参见文档说明。
对于转换,使用cast可能会获得更好的结果:

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

相关问题