免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2986 | 回复: 0
打印 上一主题 下一主题

mysql除法精度 [复制链接]

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-30 21:23 |只看该作者 |倒序浏览
I ran into this little problem today; when dividing two whole number
the result MySQL gave me was nowhere near as precise as I needed it to
be. The values were truncated which caused me some errors in my
application.
Example:
mysql> select 1*0.00001;
+-----------+
| 1*0.00001 |
+-----------+
|   0.00001 |
+-----------+
Ok, that looks fine. Let's do the same thing using division:
mysql> select 1/100000;
+----------+
| 1/100000 |
+----------+
|   0.0000 |
+----------+
Oops!  What happened?
The
issue is that the maximum precision of the result value depends on the
number of decimal places in the arguments. Since the second version
uses two whole numbers, the result uses the default number of decimal
places, which is 4. So you really have to take care to make sure you're
getting the precision you want out of your math operations!
To MySQL's credit,
the behavior is clearly documented
(however,
I maintain it's still a bit troublesome because how often would you
expect the division operator to have a list of instructions and
caveats?)
There are at least a couple simple solutions:
mysql> SELECT CAST(1/100000 AS DECIMAL(8,5) );
+---------------------------------+
| CAST(1/100000 AS DECIMAL(8,5) ) |
+---------------------------------+
|                         0.00001 |
+---------------------------------+
Or
mysql> SET div_precision_increment=5;
mysql> SELECT 1/100000;
+----------+
| 1/100000 |
+----------+
|  0.00001 |
+----------+

               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/90603/showart_2041630.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP