gpt4 book ai didi

MySQL varchar 字段之间的减法返回精度过高的小数

转载 作者:行者123 更新时间:2023-11-29 13:18:03 27 4
gpt4 key购买 nike

以下减法查询返回小数点后的位数过多。

mysql> select AA, BB, AA - BB, AA - 23.057,  22.659 - 23.057 from table1 where TID = '1';
+----------+----------+---------------------+---------------------+-----------------+
| AA | BB | AA - BB | AA - 23.057 | 22.659 - 23.057 |
+----------+----------+---------------------+---------------------+-----------------+
| 22.659 | 23.057 | -0.3979999999999997 | -0.3979999999999997 | -0.398 |
+----------+----------+---------------------+---------------------+-----------------+
1 row in set (0.00 sec)
mysql>

我希望可以使用小数字段,但用户希望保持输入时在 AA 和 BB 列中插入的精度,这就是为什么对这些列使用 VARCHAR。

e.g.:  11.10 - 11.22 = - 0.02
11.20 - 11.10 = + 0.10
22.659 - 23.057 = -0.398
23.659 - 22.559 = 0.100

while 23.659 - 22.559 = 0.1 is wrong

任何帮助将不胜感激。谢谢

最佳答案

您可以使用format()函数:

select format(col1 - col2, length(substring_index(col1, '.', -1)))

如果您不喜欢较大数字上的逗号:

select replace(format(col1 - col2, length(substring_index(col1, '.', -1))), ',', '')

关于MySQL varchar 字段之间的减法返回精度过高的小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21220977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com