views:

10032

answers:

10

I have a couple of properties in c# which are doubles 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.

+18  A: 
float

Or if you want to go old-school:

real

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 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.

richardtallent
You say float, David says decimal, now I am even more confused :)
Xaisoft
I'm using doubles in c# and these values will be latitude and longitude values in their respective columns.
Xaisoft
Great thanks for all the help.
Xaisoft
I have to concur, and admit I was off base. I was introduced to decimal in the IBM DB2 world, where decimal is a real data type, supported by all flavors of code and the database on IBM platforms. Not that it isn't a real datatype in the MS world, but it is not supported as well as in teh IBM camp. Sorry for creating any confusion.
DaveN59
+3  A: 

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.

David Basarab
Now it is even more confusing with two different answers :p
Xaisoft
Don't be confused. This answer is wrong. Decimal is a base-10 type; Float (in SQL Server and the CLR) is a base-2 type.
Michael Petrotta
No, decimal maps to decimal
Colin Mackay
+3  A: 

You should map it to FLOAT(53)- that's what LINQ to SQL does.

RichardOD
+2  A: 

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.

Euro Micelli
Actually, IIRC, double in .NET uses a 52-bit mantissa and 11-bit exponent.
richardtallent
I'll be darn; you're right! I wonder what SQL does with the extra bit; it's not used for the exponent. If it did, the exponent would go up to +-616 instead of +-308. Maybe to track NULL?
Euro Micelli
Now I'm confused. Every piece of evidence points to the idea that they use the same format (as everything else in Windows). And why wouldn't they? I can't find a definite statement on the bitwise representation in SQL Server (besides the help page for float). I'll post a correction if I find out.
Euro Micelli
+7  A: 

Here are the CLR datatype mappings to SQL Server: http://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx

Achilles
+3  A: 

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.

Matthew Jones
Double in C# is also approximate, for the same reason. So, float (or, the longer float(53)) is the best match for double.
richardtallent
+7  A: 

float is the closest equivalent.

SqlDbType Enumeration

Edit:

For Lat/Long as OP mentioned.

A metre is 1/40,000,000 of the latitude, 1 second is around 30 metres. Float/double give you 15 significant figures. With some quick and dodgy mental arithmetic... the rounding/approximation errors would be the about the length of this fill stop -> "."

gbn
+1  A: 

Aren't there already mappings in the System.Data.Sql classes?

See: C#: SQL Server data types equivalents in .NET Framework.

Now, make mine a float...

Dan Diplo
+5  A: 

As most others have noted, float is the correct answer. See Microsoft's documentation on SQL Server - CLR datatype mapping for more information.

Michael Petrotta
A: 

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.

DaveN59
As other have noted, double is imprecise as well. My vote is for float...
DaveN59
This is for storing latitude and longitude values. Does this make a difference?
Xaisoft
This is just wrong... both float/float(53) in MSSQL and double in C# have approximately 15 digits of precision. They aren't exactly the same, but close enough. Decimal, on the other hand, is complete overkill for storing a double, and it has to go back to base 2 for all calculations in SQL Server, and to come back to C# as a double.
richardtallent
I stand corrected. What you are saying makes perfect sense to me. Decimal only makes sense if you keep it as a decimal value everywhere. You lose value anytime you make a conversion from base 10 to base 2 (and back).
DaveN59