Problem
I have a couple of double attributes in C# that I want to save in a SQL Server table, but there is no double type, so which is the best to use, decimal or float?
I need the highest precision because this will record latitude and longitude values.
Thank you for your responses thus far.
Asked by Xaisoft
Solution #1
float
Or if you want to go old-school:
real
You can alternatively use float(53), which is equivalent to float.
(Note that “real” is the same as float(24), not float/float(53).)
The SQL Server type decimal(x,y) is used when you need exact decimal numbers rather than floating point numbers (which can be approximations). The C# “decimal” data type, on the other hand, is more akin to a 128-bit floating point number.
The float type in MSSQL is the same as the 64-bit double type in.NET. (My 2011 answer suggested that there might be a little difference in mantissa, but I checked this in 2020 and they appear to be 100 percent compatible in their binary representation of both very small and very large integers — see https://dotnetfiddle.net/wLX5Ox for my test.)
To add to the confusion, a “float” in C# is only 32 bits, thus it’s closer to the real/float(24) type in MSSQL than float/float in SQL (53).
In your specific use case, you only need 5 decimal places after the decimal point to represent latitude and longitude to an accuracy of around one meter, and you only need up to three digits before the decimal point to represent degrees. In MSSQL, float(24) or decimal(8,5) are the best options, while in C#, float is sufficient; you don’t require double. In fact, your users will likely appreciate the fact that you rounded to five decimal places rather than a slew of inconsequential numbers.
Answered by richardtallent
Solution #2
Also, here’s a decent SQL-CLR Type Mapping response with a helpful illustration.
From that post (by David):
Answered by Mazdak Shojaie
Solution #3
The closest equivalent is float.
SqlDbType Enumeration
As the OP mentioned, for Lat/Long.
A metre is 1/40,000,000 of a degree of latitude, and a second is approximately 30 meters. You get 15 significant figures if you float/double. The rounding/approximation errors would be roughly the length of this fill stop -> “.” with some quick and sloppy mental arithmetic.
Answered by gbn
Solution #4
You should convert it to FLOAT(53) using LINQ to SQL.
Answered by RichardOD
Solution #5
In SQL Server, float has [nearly] the precision of a “double” (in the sense of C#).
The word float is a synonym for the word float (53). The bits of the mantissa are number 53.
The mantissa in.NET double is 54 bits.
Answered by Euro Micelli
Post is based on https://stackoverflow.com/questions/1209181/what-represents-a-double-in-sql-server