What represents a double in sql server?

2019-01-03 23:52发布

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?

This will store latitude and longitude values, so I need the most accurate precision.

Thanks for the responses so far.

14条回答
仙女界的扛把子
2楼-- · 2019-01-04 00:10

float in SQL Server actually has [edit:almost] the precision of a "double" (in a C# sense).

float is a synonym for float(53). 53 is the bits of the mantissa.

.NET double uses 54 bits for the mantissa.

查看更多
Luminary・发光体
3楼-- · 2019-01-04 00:10

@Achilles Excellent! Came here for the double stayed for the TinyInt.

Here is a partially done switch case to convert between dataTable and SQL:

switch (columnDataTypeList[i])
{
    case "System.String":
        columnDataTypeList[i] = "VARCHAR(MAX)";
        break;
    case "System.Int32":
        columnDataTypeList[i] = "INT";
        break;
    case "System.DateTime":
        columnDataTypeList[i] = "DATE";
        break;
    case "System.Double":
        columnDataTypeList[i] = "FLOAT";
        break;
}

The string can be a problem be sure to edit it to your needs, but the double should work which is the question at hand.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-04 00:11

Also, here is a good answer for SQL-CLR Type Mapping with a useful chart.

From that post (by David): enter image description here

查看更多
萌系小妹纸
5楼-- · 2019-01-04 00:11

In case this is of use, the below is the link I usually refer to (I ended up on this thread on a first Google this time!).

It has information additional to the previous links posted, as it shows the SqlDataReader procedures and enumerations (in addition to the .NET and SQL type comparisons)

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings

(and yes float!)

查看更多
Fickle 薄情
6楼-- · 2019-01-04 00:12

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.

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.

查看更多
你好瞎i
7楼-- · 2019-01-04 00:16

For SQL Sever:

Decimal Type is 128 bit signed number Float is a 64 bit signed number.

The real answer is Float, I was incorrect about decimal.

The reason is if you use a decimal you will never fill 64 bit of the decimal type.

Although decimal won't give you an error if you try to use a int type.

Here is a nice reference chart of the types.

查看更多
登录 后发表回答