3

I am attempting to use Entity Framework and have a contact database that has Longitude and Latitude data from Google Maps.

The guide says that this should be stored as float.

I have created my POCO entity which includes Longitude as a float and Latitude as a float.

I have just noticed that in the database, these both come up as real.

There is still a lot of work to be done before I can get any data back from Google, I am very far away from testing and I was just wondering if anyone can tell me if this is going to be a problem later on?

wil
  • 2,019
  • 2
  • 15
  • 14

2 Answers2

6

Nope, that should be fine. Note that you may not get the exact same value back as you received from Google Maps, as that would have been expressed in decimal-formatted text. However, this is effectively a physical quantity, and thus more appropriate as a float/double/real/(whatever binary floating point type you like) than as a decimal. (Decimals are more appropriate for man-made quantities - particularly currency.)

If you look at the documentation for float and real you'll see that real is effectively float(24) - a 32-bit floating binary point type, just like float in C#.

EDIT: As noted in comments, if you want more than the significant 7-8 digits of accuracy provided by float, then you probably want double instead in C#, which would mean float(53) in SQL Server.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the explanation, but, I just manually input the data in to the database and as you said, it didn't store the exact value... a random location of 41.710855, -93.959198 was stored as 41.71085, -93.9592 ... I just tried doing a search and these locations appear to be about 3/4 of a mile out... This is a little bit to far :( ... I am guessing that this may be a difference between different language's definition of float? (I was trying to work from this example - http://code.google.com/apis/maps/articles/phpsqlsearch.html ) ... Is there a workaround? – wil Sep 23 '11 at 06:30
  • @wil: Well, if you want more than 7-8 significant digits, use `double` in the C# and `float(53)` in the database. – Jon Skeet Sep 23 '11 at 06:34
  • Sorry, didn't realise that this would lead to a different question, but, thank you. – wil Sep 23 '11 at 06:41
  • Why EF doesn't allow to set precision on float but allows for decimal? If a property is of float then I can't set its precision. – dragonfly02 Nov 08 '15 at 21:18
  • @stt106: Well if you follow the documentation, it looks like there are really only two values for the precision of `float/real` - which correspond to using `float` or `double` in C#. – Jon Skeet Nov 09 '15 at 06:44
2

This link:

http://msdn.microsoft.com/en-us/library/aa258876(v=sql.80).aspx

explains that, in SQL Server, real is a synonym for float(24), using 4 bytes of data. In .NET a Single precision floating point number also uses 4 bytes, so these are pretty much equivalent:

http://msdn.microsoft.com/en-us/library/47zceaw7(v=vs.71).aspx

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158