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.
There is a great thread on MSDN describing the primary difference between FLOAT and DECIMAL. In short, Float is approximate and cannot represent some values.
Look at the accepted answer.
Or if you want to go old-school:
You can also use float(53), but it means the same thing as float.
("real" is equivalent to float(24), not 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). This is in contrast to the C# "decimal" data type, which is more like a 128-bit floating point number.
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.
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).
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.