Mysql十进制: floor instead of round

标签 mysql decimal

在我的 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

SQLFiddle demo

关于Mysql十进制: floor instead of round,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936261/

相关文章:

php - 如何使用 php 将输入字段数组插入到 mysql 中

mysql - Google Cloud SQL - 创建故障转移失败

mysql - where 子句中小写

php - 在 html 中显示数据库中的特定值

c++ - 如何在使用模数将十进制值转换为货币时使程序编译?

Python函数去除小数点右边的零,包括小数点?

ruby-on-rails - to_d 在 ruby​​ 中总是返回 2 个小数位

java - 无法在 mysql DB 中保留 1970 年以下的日期

java - 打开导出的 CSV 文件时,Excel 不显示小数点零

python - 在 python 中获取 Decimal 的 Ceil()?