SQL Server 自动舍入?

发布于 2024-07-23 08:23:56 字数 605 浏览 4 评论 0原文

我对以下结果感到非常困惑:

PRINT 3.1415926535897931 /180

控制台结果 = 0.01745329251994329500

DECLARE @whatTheHell float(53)
SET @whatTheHell  = 3.1415926535897931/180
PRINT @whatTheHell 

控制台结果 = 0.0174533

我不明白,因为参考此:

http://msdn.microsoft.com/en-us/library/ms131092.aspx

Sql Server Float 应该相当于 c# double。 但是当我在 c# 中计算时:

double hellYeah = 3.1415926535897931 /180;

我得到 0.017453292519943295...

I am very confused by the following results:

PRINT 3.1415926535897931 /180

Console result = 0.01745329251994329500

DECLARE @whatTheHell float(53)
SET @whatTheHell  = 3.1415926535897931/180
PRINT @whatTheHell 

Console result = 0.0174533

I don't understand because referring to this:

http://msdn.microsoft.com/en-us/library/ms131092.aspx

Sql Server Float should be equivalent to c# double.
But when I compute this in c#:

double hellYeah = 3.1415926535897931 /180;

I get 0.017453292519943295...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

不必你懂 2024-07-30 08:23:57

我认为您对 PRINT 使用 STR 函数的默认设置(长度为 10)隐式将数字转换为字符这一事实感到困惑(请参阅MSDN)。 尝试 PRINT STR(@wth, 20, 16),您可能会更高兴。

I think you're getting confused by the fact that PRINT implicitly converts numeric to character with the default setting for the STR function -- a length of 10 (see MSDN). Try PRINT STR(@wth, 20, 16) and you might be happier.

梦魇绽荼蘼 2024-07-30 08:23:57

除法不是四舍五入。 PRINT 是四舍五入。

DECLARE
  @var1 float,
  @var2 float,
  @var3 float

SET @var1 = 3.1415926535897931
SET @var2 = 180

SET @var3 = @var1 / @var2
SELECT @var1/@var2 as Computed, @var3 as FromVariable

PRINT @var1/@var2
PRINT @var3

Divide is not rounding. PRINT is rounding.

DECLARE
  @var1 float,
  @var2 float,
  @var3 float

SET @var1 = 3.1415926535897931
SET @var2 = 180

SET @var3 = @var1 / @var2
SELECT @var1/@var2 as Computed, @var3 as FromVariable

PRINT @var1/@var2
PRINT @var3
闻呓 2024-07-30 08:23:57

我猜“FLOAT”类型只是对精度有限制。 Books Online 说使用 FLOAT(53) 您应该获得最多 15 位的精度 - 不确定这些数字是在小数点分隔符之前还是之后是否存在固有限制。

尝试使用小数代替:

DECLARE @whatTheHell2 decimal(18,16)
SET @whatTheHell2  = 3.1415926535897931/180
PRINT @whatTheHell2 

给我结果:

0.0174532925199433

Marc

I guess the "FLOAT" type just has a limit on precision. Books Online says with FLOAT(53) you should get up to 15 digits of precision - not sure if there's an inherent limitation whether those digits are before or after the decimal separator.

Try using decimal instead:

DECLARE @whatTheHell2 decimal(18,16)
SET @whatTheHell2  = 3.1415926535897931/180
PRINT @whatTheHell2 

Gives me the result:

0.0174532925199433

Marc

你的往事 2024-07-30 08:23:57

来自 SQL Server 2005 联机丛书 数据类型转换< /a> 主题:

在 Transact-SQL 语句中,常量
自动带有小数点
转换为数值数据值,
使用最小精度和小数位数
必要的。 例如,常数
12.345 转换为精度为 5 的数值
规模为 3。

因此以下内容更能代表 SQL Server 隐式执行的操作:

DECLARE @whatTheHell NUMERIC(21, 20)
SET @whatTheHell = 3.1415926535897931 / 180
PRINT @whatTheHell

From the SQL Server 2005 Books Online Data Type Conversion topic:

In Transact-SQL statements, a constant
with a decimal point is automatically
converted into a numeric data value,
using the minimum precision and scale
necessary. For example, the constant
12.345 is converted into a numeric value with a precision of 5 and a
scale of 3.

So the following is more representative of what SQL Server is doing implicitly:

DECLARE @whatTheHell NUMERIC(21, 20)
SET @whatTheHell = 3.1415926535897931 / 180
PRINT @whatTheHell
行至春深 2024-07-30 08:23:57

PRINT 3.1415926535897931 /180 被评估为十进制。

Float 仅解析为 15 位有效数字。 你有 17 个,所以它不能是浮动的。 由于数据类型优先级和输出,180 通过隐式转换变成十进制比例和精度基于这些规则

输出0.01745329251994329500有17个sig无花果也是。 它必须是十进制。

现在,SET @whatTheHell = 3.1415926535897931/180。 浮点转换作为赋值运算符的一部分进行。 在此之前,它的右侧也是小数。 浮点数是近似值,并且是四舍五入。

在 C# 中,它都是双精度数,因为你没有固定点(除非你告诉编译器?)

相关问题:

为小数(x,y)选择适当的精度

在 SQL 中,如何将货币数据类型转换为小数?

SQL Server,where 子句与不同类型的比较 默认转换行为

PRINT 3.1415926535897931 /180 is being evaluated as decimal.

Float only resolves to 15 significant figures. You have 17 so it can't be float. The 180 becomes decimal through implicit conversion because of datatype precedence and the output scale and precision is based on these rules

The output 0.01745329251994329500 has 17 sig figs too. It must be decimal.

Now, SET @whatTheHell = 3.1415926535897931/180. The float conversion takes place as part of the assignment operator. Before that it is also decimal on the right hand side. Float is approximate, and it's rounding.

In c# it's all doubles because you don't have fixed point (unless you tell the compiler?)

Related questions:

Choosing the appropriate precision for decimal(x,y)

In SQL how can I convert a money datatype to a decimal?

SQL Server, where clauses comparisons with different types & default casting behaviour

拥有 2024-07-30 08:23:57

当您说 SQL 浮点映射到 C# 双精度时,我认为您假设 SQL 浮点字节大小与 C# 双字节大小相同。

我想说,C# double 是 C# 中唯一足够大的浮点数据类型,可以存储 SQL 浮点数。

示例:

C# Double = 8 字节
Sql Float = 4 bytes

解决问题的简单方法是在 SQL 中使用小数或数字

When you say that SQL float maps to C# double, I think you are assuming that the SQL float byte size is the same as the C# double byte size.

I would say that the C# double is the only floating point data type big enough, in C#, to store the SQL float.

Example:

C# Double = 8 Bytes
Sql Float = 4 bytes

The easy fix for your problem is to use decimal or numeric in your SQL

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