SQL Server 中的划分
在 SQL Server 2005 Express 中,结果如下
选择 100 / 15 --结果 6
但我想得到近似值 7 (就像使用计算器一样)
100 / 15 = 6.6666..
如何在 SQL Server 中实现它?
In SQL Server 2005 Express, the result is below
SELECT 100 / 15 --Result 6
But I wanna get approximate value 7 (like using calculator)
100 / 15 = 6.6666..
How to make it in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须使用十进制数字,例如 100 / 15.0。如果使用整数,结果将被解释为整数并被截断为小于或等于结果的最大整数。
You have to use decimal numbers e.g. 100 / 15.0. If you use integers the result is interpreted as an integer and truncated to the largest integer that is smaller than or equal to the result.
SELECT 转换(100 作为浮点数)/(15 作为浮点数)
SELECT cast(100 as float) / (15 as float)
您希望 SQL Server 执行浮点除法,而不是整数除法。如果您使用文字值(如示例所示),请在分母后添加
.0
来强制 SQL Server 将其视为浮点值。否则,请确保将变量声明为
FLOAT
。You want SQL Server to perform floating-point divison, as opposed to integer division. If you're using literal values, as in your example, suffix the denominator with
.0
to force SQL Server to treat it as a floating-point value.Otherwise, make sure you declare your variables as
FLOAT
.尝试将变量声明为浮点数:
Try declaring variables as floats:
你需要乘以0.1
然后将结果乘以 10
以获得准确的结果。例子:
(100*0.1/15)*10
结果将为您 6.666660。
You need to multiply it with 0.1
and than multiply 10 to the whole result
for getting the exact result. Example:
(100*0.1/15)*10
will give you 6.666660 as result.