用十进制除以十进制并得到零 [英] Divide decimal by decimal and get zero

查看:77
本文介绍了用十进制除以十进制并得到零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要做:

select CAST(1 AS DECIMAL(38, 28))/CAST(1625625 AS DECIMAL(38, 28))

我得到0,但是我得到

select CAST(1 AS DECIMAL(20, 10))/CAST(1625625 AS DECIMAL(20, 10))

我有价值吗?

我本来希望更高的精度除法会返回更高的精度结果,为什么不是这种情况呢?

I would have expected the higher precision division to return a higher precision result, why is this not the case?

推荐答案

之所以会发生这种情况,是因为对于数学运算,使用

This happens because for math operations the resulting precision and scale is calculated with the rules found in http://msdn.microsoft.com/en-us/library/ms190476.aspx#division

操作
e1/e2

operation
e1 / e2

结果精度
p1-s1 + s2 + max(6,s1 + p2 + 1)

结果量表
max(6,s1 + p2 + 1)

因此,当两个部分都使用decimal(38/28)时,您将以不支持的decimal(105,67)结尾,因此它会被截断.

So when using decimal(38/28) for both parts then you end with decimal(105,67) which is not supported so it get truncated.

也引用

Also quoting

结果精度和小数位的绝对最大值为38.当结果精度大于38时,将减小相应的小数位,以防止结果的整数部分被截断.

The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.

这篇关于用十进制除以十进制并得到零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆