本文翻译自:What represents a double in sql server?
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. 感谢到目前为止的回复。
#1楼
参考:https://stackoom.com/question/54Yv/什么代表sql-server中的double
#2楼
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 exact decimal numbers rather than floating point (which can be approximations). 十进制(x,y) SQL Server类型适用于需要精确十进制数而不是浮点数(可以是近似值)的情况。 This is in contrast to the C# "decimal" data type, which is more like a 128-bit floating point number. 这与C#“十进制”数据类型形成对比,后者更像是一个128位浮点数。
MSSQL float does not have exactly the same precision as the 64-bit double type in .NET (slight difference in mantissa IIRC), but it's a close enough match for most uses. MSSQL float与.NET中的64位double类型没有完全相同的精度(尾数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. 在您的特定用例中...您需要的只是小数点后面的5个位置来表示大约一米精度内的纬度和经度,并且您只需要在小数点前最多三位数。 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. Float(24)或十进制(8,5)最适合您在MSSQL中的需求,并且在C#中使用float是足够好的,您不需要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位,而不是为了骑行而出现一堆无关紧要的数字。
#3楼
For SQL Sever: 对于SQL Sever:
Decimal Type is 128 bit signed number Float is a 64 bit signed number. 十进制类型是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不会给出错误。
Here is a nice reference chart of the types. 这是一个很好的参考图表类型。
#4楼
你应该将它映射到FLOAT(53) - 这就是LINQ to SQL的作用 。
#5楼
float
in SQL Server actually has [edit:almost] the precision of a "double" (in a C# sense). SQL Server中的float
实际上有[编辑:几乎]精度为“double”(在C#意义上)。
float
is a synonym for float(53)
. float
是float(53)
的同义词。 53 is the bits of the mantissa. 53是尾数的位。
.NET double
uses 54 bits for the mantissa. .NET double
使用54位作为尾数。
#6楼
float is the closest equivalent. float是最接近的等价物。
SqlDbType Enumeration SqlDbType枚举
For Lat/Long as OP mentioned. 对于Lat / Long,如OP所述。
A metre is 1/40,000,000 of the latitude, 1 second is around 30 metres. 一米是纬度的1 / 40,000,000,1秒是30米左右。 Float/double give you 15 significant figures. Float / double给你15个有效数字。 With some quick and dodgy mental arithmetic... the rounding/approximation errors would be the about the length of this fill stop -> "." 有一些快速和狡猾的心算...圆角/近似误差将是这个填充停止的长度 - >“。”