为什么除法返回零

发布于 2024-08-10 17:00:18 字数 229 浏览 4 评论 0原文

这是我在示例中使用的代码:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

树深时见影 2024-08-17 17:00:18

将 set1 和 set2 声明为浮点数而不是整数,或者将它们转换为浮点数作为计算的一部分:

SET @weight= CAST(@set1 AS float) / CAST(@set2 AS float);

Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation:

SET @weight= CAST(@set1 AS float) / CAST(@set2 AS float);
您的好友蓝忘机已上羡 2024-08-17 17:00:18

当您在除法中仅使用整数时,您将得到整数除法(= 结果为整数)。当您使用(至少一个)double或float时,您将得到浮点除法(以及您想要得到的答案)。

因此,您可以

  1. 将一个或两个变量声明为 float/double
  2. 将一个或两个变量强制转换为 float/double。

不要只是将整数除法的结果转换为双精度:除法已经作为整数除法执行,因此小数点后面的数字已经丢失。

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

  1. declare one or both of the variables as float/double
  2. cast one or both of the variables to float/double.

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.

美人迟暮 2024-08-17 17:00:18

只需将除法的底部乘以 1.0(或任意数量的小数位数)

PRINT @set1 
PRINT @set2 
SET @weight= @set1 / @set2 *1.00000; 
PRINT @weight

Simply mutiply the bottom of the division by 1.0 (or as many decimal places as you want)

PRINT @set1 
PRINT @set2 
SET @weight= @set1 / @set2 *1.00000; 
PRINT @weight
安静 2024-08-17 17:00:18

因为它是一个整数。您需要将它们声明为浮点数或小数,或在计算中转换为浮点数或小数。

Because it's an integer. You need to declare them as floating point numbers or decimals, or cast to such in the calculation.

情绪操控生活 2024-08-17 17:00:18

如果您将其声明为浮点或任何十进制格式,它将显示

0

例如:

declare @weight float;

SET @weight= 47 / 638; PRINT @weight

输出:0

如果您希望输出为

0.073667712

例如

declare @weight float;

SET @weight= 47.000000000 / 638.000000000; PRINT @weight

if you declare it as float or any decimal format it will display

0

only

E.g :

declare @weight float;

SET @weight= 47 / 638; PRINT @weight

Output : 0

If you want the output as

0.073667712

E.g

declare @weight float;

SET @weight= 47.000000000 / 638.000000000; PRINT @weight
迟月 2024-08-17 17:00:18

在 SQL Server 中,两个整数直接相除会返回整数,即使结果应该是浮点数。下面有一个示例可以帮助您理解:

--1--
declare @weird_number_float float
set @weird_number_float=22/7
select @weird_number_float

--2--
declare @weird_number_decimal decimal(18,10)
set @weird_number_decimal=22/7 
select @weird_number_decimal

--3--
declare @weird_number_numeric numeric
set @weird_number_numeric=22/7 
select @weird_number_numeric

--Right way

declare @weird_number float
set @weird_number=cast(22 as float)/cast(7 as float)
select @weird_number

最后一个块将返回 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:

--1--
declare @weird_number_float float
set @weird_number_float=22/7
select @weird_number_float

--2--
declare @weird_number_decimal decimal(18,10)
set @weird_number_decimal=22/7 
select @weird_number_decimal

--3--
declare @weird_number_numeric numeric
set @weird_number_numeric=22/7 
select @weird_number_numeric

--Right way

declare @weird_number float
set @weird_number=cast(22 as float)/cast(7 as float)
select @weird_number

Just last block will return the 3,14285714285714. In spite of the second block defined with right precision the result will be 3.00000.

痕至 2024-08-17 17:00:18
 CAST(@set1 AS float)
 CAST(@set2 AS float)

convert(float,@set1)
convert(float,@set2)
                    

首先转换为浮点数然后应用运算符

 CAST(@set1 AS float)
 CAST(@set2 AS float)

or

convert(float,@set1)
convert(float,@set2)
                    

First convert into float then apply operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文