为什么除法返回零
这是我在示例中使用的代码:
PRINT @set1
PRINT @set2
SET @weight= @set1 / @set2;
PRINT @weight
这是结果:
47
638
0
我想知道为什么它返回 0
而不是 0,073667712
Here is the code I'm using in the example:
PRINT @set1
PRINT @set2
SET @weight= @set1 / @set2;
PRINT @weight
Here is the result:
47
638
0
I would like to know why it's returning 0
instead of 0,073667712
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将 set1 和 set2 声明为浮点数而不是整数,或者将它们转换为浮点数作为计算的一部分:
Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation:
当您在除法中仅使用整数时,您将得到整数除法(= 结果为整数)。当您使用(至少一个)double或float时,您将得到浮点除法(以及您想要得到的答案)。
因此,您可以
不要只是将整数除法的结果转换为双精度:除法已经作为整数除法执行,因此小数点后面的数字已经丢失。
When you use only integers in a division, you will get integer division (= resulting in an integer). When you use (at least one) double or float, you will get floating point division (and the answer you want to get).
So you can
Do not just cast the result of the integer division to double: the division was already performed as integer division, so the numbers behind the decimal are already lost.
只需将除法的底部乘以 1.0(或任意数量的小数位数)
Simply mutiply the bottom of the division by 1.0 (or as many decimal places as you want)
因为它是一个整数。您需要将它们声明为浮点数或小数,或在计算中转换为浮点数或小数。
Because it's an integer. You need to declare them as floating point numbers or decimals, or cast to such in the calculation.
如果您将其声明为浮点或任何十进制格式,它将显示
仅
例如:
输出:0
如果您希望输出为
例如
if you declare it as float or any decimal format it will display
only
E.g :
Output : 0
If you want the output as
E.g
在 SQL Server 中,两个整数直接相除会返回整数,即使结果应该是浮点数。下面有一个示例可以帮助您理解:
最后一个块将返回 3,14285714285714。尽管以正确的精度定义了第二个块,结果仍将是 3.00000。
In SQL Server direct division of two integer returns integer even if the result should be the float. There is an example below to get it across:
Just last block will return the 3,14285714285714. In spite of the second block defined with right precision the result will be 3.00000.
或
首先转换为浮点数然后应用运算符
or
First convert into float then apply operator