Mysql decimal: floor instead of round

痞子三分冷 提交于 2019-12-10 14:24:28

问题


In my MySQL database I have field DECIMAL(23,5), so 5 digits after decimal point. Now, when I will query like this:

UPDATE my_table SET my_decimal_field = 123.123456789 WHERE id = 1

And then I will fetch that record:

SELECT id, my_decimal_field FROM gijhars WHERE id = 1

I get this result:

+------+------------------+
| id   | my_decimal_field |
+------+------------------+
| 5733 |   123.12346      |
+------+------------------+

So, of course, MySQL is rounding these values, if they have more than 6 digits after decimal point. Is there any setting in MySQL, to floor these values instead of rounding?


回答1:


From the 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.)

If that is not the case for your operating system, then you can handle this while updating with truncate

UPDATE my_table 
SET my_decimal_field = truncate(123.123456789, 5) 
WHERE id = 1

SQLFiddle demo



来源:https://stackoverflow.com/questions/14936261/mysql-decimal-floor-instead-of-round

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!