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.
float
in SQL Server actually has [edit:almost] the precision of a "double" (in a C# sense).float
is a synonym forfloat(53)
. 53 is the bits of the mantissa..NET
double
uses 54 bits for the mantissa.@Achilles Excellent! Came here for the double stayed for the TinyInt.
Here is a partially done switch case to convert between dataTable and SQL:
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.
Also, here is a good answer for SQL-CLR Type Mapping with a useful chart.
From that post (by David):
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!)
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.
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.