在我的 MySQL 数据库中,我有字段 DECIMAL(23,5),所以小数点后有 5 位数字。现在,当我这样查询时:
UPDATE my_table SET my_decimal_field = 123.123456789 WHERE id = 1
然后我将获取该记录:
SELECT id, my_decimal_field FROM gijhars WHERE id = 1
我得到这个结果:
+------+------------------+
| id | my_decimal_field |
+------+------------------+
| 5733 | 123.12346 |
+------+------------------+
因此,如果小数点后超过 6 位,MySQL 当然会舍入这些值。 MySQL 中是否有任何设置floor 这些值而不是四舍五入?
最佳答案
来自 doc
When such a column is assigned a value with more digits following the decimal point than are permitted by the specified scale, the value is converted to that scale. (The precise behavior is operating system-specific, but generally the effect is truncation to the permissible number of digits.)
如果您的操作系统不是这种情况,那么您可以在使用 truncate 更新时处理此问题
UPDATE my_table
SET my_decimal_field = truncate(123.123456789, 5)
WHERE id = 1
关于Mysql十进制: floor instead of round,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936261/