关于mysql的trancate()函授的疑问

mathematician 2004-10-21 03:09:26
SELECT truncate(2139.14,2);

结果为什么是2139.13?应该是2139.14才对吧?
...全文
160 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mathematician 2004-11-24
  • 打赏
  • 举报
回复
又回来了,提一下!
mathematician 2004-11-19
  • 打赏
  • 举报
回复
up!
mathematician 2004-11-16
  • 打赏
  • 举报
回复
看看这条SQL语句好吗?
select truncate(currency/100000000,8) as currency from tablename;
其中tablename表中字段currency为整型,现在需要将该字段除以100000000后保留8位小数查询出来。

遇到的问题就是:这样查询出来的结果有时候会不正确,请问大家如何解决或者回避这种计算字段的问题?有没有别的函数可以用来实现此种功能吗?
shuixin13 2004-11-15
  • 打赏
  • 举报
回复
呵呵.
mysql> SELECT truncate(2139.12,2);
+---------------------+
| truncate(2139.12,2) |
+---------------------+
| 2139.11 |
+---------------------+
1 row in set (0.00 sec)

此语句中的 2139.12 因为是浮点数.
而浮点数在计算机中是非精确的.
所以可能会出现上面所示的情况
mathematician 2004-11-15
  • 打赏
  • 举报
回复
谢谢bluefiend(<蓝魔之泪>)。

大家看到了,在结果中有部分数据是不正确的,请问应该怎么解决?
gimy007 2004-11-12
  • 打赏
  • 举报
回复
select version();
bluefiend 2004-11-12
  • 打赏
  • 举报
回复
MYSQL 版本 在哪里看啊?
bluefiend 2004-11-12
  • 打赏
  • 举报
回复
mysql> SELECT truncate(2139.10,2);
+---------------------+
| truncate(2139.10,2) |
+---------------------+
| 2139.09 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT truncate(2139.11,2);
+---------------------+
| truncate(2139.11,2) |
+---------------------+
| 2139.11 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT truncate(2139.12,2);
+---------------------+
| truncate(2139.12,2) |
+---------------------+
| 2139.11 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT truncate(2139.13,2);
+---------------------+
| truncate(2139.13,2) |
+---------------------+
| 2139.13 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT truncate(2139.14,2);
+---------------------+
| truncate(2139.14,2) |
+---------------------+
| 2139.13 |
+---------------------+
1 row in set (0.00 sec)
mathematician 2004-11-12
  • 打赏
  • 举报
回复
up!
mathematician 2004-11-10
  • 打赏
  • 举报
回复
我的是4.0,你试验
SELECT truncate(2139.10,2);
SELECT truncate(2139.11,2);
SELECT truncate(2139.12,2);
SELECT truncate(2139.13,2);
SELECT truncate(2139.14,2);
看看结果好吗?
DeltaCat 2004-10-23
  • 打赏
  • 举报
回复
你的 MYSQL 版本多少? 我的 4.20 正常啊, 就是 2139.14
mathematician 2004-10-21
  • 打赏
  • 举报
回复
up

56,803

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧