C# 什么代表sql server中的double?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1209181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What represents a double in sql server?
提问by Xaisoft
I have a couple of properties in C#
which are double
and I want to store these in a table in SQL Server, but noticed there is no double
type, so what is best to use, decimal
or float
?
我有一对夫妇在性C#
这是double
我想将这些存储在SQL Server中的表,但发现没有double
类型,所以什么是最好的使用,decimal
还是float
?
This will store latitude and longitude values, so I need the most accurate precision.
这将存储纬度和经度值,所以我需要最准确的精度。
Thanks for the responses so far.
感谢您到目前为止的答复。
采纳答案by richardtallent
float
Or if you want to go old-school:
或者,如果你想上学:
real
You can also use float(53), but it means the same thing as float.
您也可以使用 float(53),但它的含义与 float 相同。
("real" is equivalent to float(24), not float/float(53).)
(“real”相当于 float(24),而不是 float/float(53)。)
The decimal(x,y)SQL Server type is for when you want exactdecimal numbers rather than floating point (which can be approximations). This is in contrast to the C# "decimal" data type, which is more like a 128-bit floating point number.
该小数(X,Y)的SQL Server类型是当你想确切的十进制数字,而不是浮点数(可近似)。这与 C#“十进制”数据类型形成对比,后者更像是 128 位浮点数。
MSSQL floatdoes not have exactlythe same precision as the 64-bit doubletype in .NET (slight difference in mantissa IIRC), but it's a close enough match for most uses.
MSSQL浮点数与.NET 中的 64 位双精度类型的精度不完全相同(尾数 IIRC 略有不同),但对于大多数用途来说,它是足够接近的匹配。
To make things more confusing, a "float" in C# is only 32-bit, so it would be more equivalent in SQL to the real/float(24) type in MSSQL than float/float(53).
更令人困惑的是,C# 中的“float”只有 32 位,因此它在 SQL 中与 MSSQL 中的 real/float(24) 类型比在 float/float(53) 中更等效。
In your specific use case...All you need is 5 places after the decimal point to represent latitude and longitude within about one-meter precision, and you only need up to three digits before the decimal point for the degrees. Float(24) or decimal(8,5) will best fit your needs in MSSQL, and using float in C# is good enough, you don't need double. In fact, your users will probably thank you for rounding to 5 decimal places rather than having a bunch of insignificant digits coming along for the ride.
在您的特定用例中...您只需要小数点后 5 位来表示大约一米精度的纬度和经度,而度数只需要小数点前三位数字。Float(24) 或 decimal(8,5) 最适合您在 MSSQL 中的需求,并且在 C# 中使用 float 就足够了,您不需要 double。事实上,您的用户可能会感谢您四舍五入到小数点后 5 位而不是一堆无关紧要的数字。
回答by David Basarab
For SQL Sever:
对于 SQL 服务器:
Decimal Type is 128 bit signed number Float is a 64 bit signed number.
Decimal Type 是 128 位有符号数 Float 是 64 位有符号数。
The real answer is Float, I was incorrect about decimal.
真正的答案是Float,我对十进制的理解是错误的。
The reason is if you use a decimal you will never fill 64 bit of the decimal type.
原因是如果您使用十进制,您将永远不会填充十进制类型的 64 位。
Although decimal won't give you an error if you try to use a int type.
尽管如果您尝试使用 int 类型,decimal 不会给您错误。
Hereis a nice reference chart of the types.
这是一个很好的类型参考图表。
回答by RichardOD
You should map it to FLOAT(53)- that's what LINQ to SQL does.
你应该将它映射到 FLOAT(53)——这就是LINQ to SQL 所做的。
回答by Euro Micelli
float
in SQL Server actually has [edit:almost] the precision of a "double" (in a C# sense).
float
在 SQL Server 中实际上具有 [编辑:几乎] 精度的“双精度”(在 C# 意义上)。
float
is a synonym for float(53)
. 53 is the bits of the mantissa.
float
是 的同义词float(53)
。53 是尾数位。
.NET double
uses 54 bits for the mantissa.
.NETdouble
使用 54 位作为尾数。
回答by gbn
float is the closest equivalent.
float 是最接近的等价物。
For Lat/Long as OP mentioned.
对于 OP 提到的纬度/经度。
A metre is 1/40,000,000 of the latitude, 1 second is around 30 metres. Float/double give you 15 significant figures. With some quick and dodgy mental arithmetic... the rounding/approximation errors would be the about the length of this fill stop -> "."
一米是纬度的 1/40,000,000,1 秒大约是 30 米。Float/double 给你 15 个有效数字。通过一些快速而狡猾的心算......舍入/近似误差将是这个填充停止的长度 - >“。”
回答by DaveN59
It sounds like you can pick and choose. If you pick float, you may lose 11 digits of precision. If that's acceptable, go for it -- apparently the Linq designers thought this to be a good tradeoff.
听起来你可以挑选。如果选择浮点数,则可能会丢失 11 位精度。如果这是可以接受的,那就去做吧——显然,Linq 设计师认为这是一个很好的权衡。
However, if your application needs those extra digits, use decimal. Decimal (implemented correctly) is way more accurate than a float anyway -- no messy translation from base 10 to base 2 and back.
但是,如果您的应用程序需要这些额外的数字,请使用十进制。无论如何,十进制(正确实现)比浮点数更准确 - 从基数 10 到基数 2 再返回没有混乱的转换。
回答by Mazdak Shojaie
回答by Md Shoriful Islam
A Float
represents double
in SQL server. You can find a proof from the coding in C# in visual studio. Here I have declared Overtime
as a Float
in SQL server and in C#. Thus I am able to convert
A在 SQL 服务器中Float
表示double
。您可以从 Visual Studio 中的 C# 编码中找到证明。在这里,我在 SQL Server 和 C# 中声明Overtime
为 a Float
。因此我能够转换
int diff=4;
attendance.OverTime = Convert.ToDouble(diff);
Here OverTime
is declared float type
这里OverTime
声明float type