SQL Server 自动舍入?
我对以下结果感到非常困惑:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您对
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 theSTR
function -- a length of 10 (see MSDN). TryPRINT STR(@wth, 20, 16)
and you might be happier.除法不是四舍五入。 PRINT 是四舍五入。
Divide is not rounding. PRINT is rounding.
我猜“FLOAT”类型只是对精度有限制。 Books Online 说使用 FLOAT(53) 您应该获得最多 15 位的精度 - 不确定这些数字是在小数点分隔符之前还是之后是否存在固有限制。
尝试使用小数代替:
给我结果:
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:
Gives me the result:
Marc
来自 SQL Server 2005 联机丛书 数据类型转换< /a> 主题:
因此以下内容更能代表 SQL Server 隐式执行的操作:
From the SQL Server 2005 Books Online Data Type Conversion topic:
So the following is more representative of what SQL Server is doing implicitly:
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
当您说 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