1

I need to store the following double value in SQL Server:

double x = 52.22105994970536;

What SQL Server datatype should I use to store values of this type. Perhaps decimal or float?

I am not sure if this is relevant but I need to store these values with a . not a , to separate the fractional portion of the values. Is there a setting in SQL Server that I should be aware of to ensure this happens?

codingbadger
  • 42,678
  • 13
  • 95
  • 110
whoah
  • 4,363
  • 10
  • 51
  • 81
  • 2
    You're confusing data types and their *conventional* string representations. If you store as e.g. `decimal`, `float` or `real`, you don't store a `.` (or a `,`). Those things only appear when the values are formatted into strings. – Damien_The_Unbeliever Jul 09 '12 at 10:54
  • http://stackoverflow.com/questions/1209181/what-represents-a-double-in-sql-server – Pluc Jul 09 '12 at 10:54
  • Possible duplicate of [What represents a double in sql server?](https://stackoverflow.com/questions/1209181/what-represents-a-double-in-sql-server) – Erik Apr 09 '18 at 22:59

2 Answers2

1

I am not sure if this is relevant but I need to store these values with a . not a , to separate the fractional portion of the values. Is there a setting in SQL Server that I should be aware of to ensure this happens?

No, it is totally enough to learn programming to the point you realize that this is not a question at all - decimals are stored as decimals. "." or "," are part of the visual conversion (the "ToString" call, so to say) to print the value and have nothing to do with the value.

If you want to store a double, you want to store a double. Point. If you want to make sure your program presents it with a ".", then PROGRAM THE UI PROPERLY, but do not bother SQL Server internal storage with it. Normally they are shown in the locale - which is smarter than hardcoding in most cases. SO, maybvbe you force-change the UI locale? Or hardcode the conversion to apply every time you print out a value.

What SQL Server datatype should I use to store values of this type. Perhaps decimal or float?

http://msdn.microsoft.com/en-us/library/ms187752.aspx

explains the data types of sql server.

Choose one that fits your requiremnents. Likely a float version with a given precision. Now, if you ar afraid because those are named as "approximate numeric" note that a double IS an approximate numeric, also in C# (or any other front end language you use - you do not tell us).

Default recommended mappings are at http://msdn.microsoft.com/en-us/library/ms131092.aspx and would point towards a "float".

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • I didn't realize that this "," is only visual conversion - thanks for answer, it is very helpful for me :) so, in conclusion: If I want to store this value (from C#) into database: `double x = 52.22105994970536;` I have to put this into column with float type, right? And then, when I will read this value from database in some function, it will have "." instead of ",", yep? Greetings! – whoah Jul 09 '12 at 11:22
  • No. It wont have neither "." nor ",", it is stored in a binary format that has no knowlege of either of those. THey only appear whn you convert it to a string, but they are not intrinsic to the number at all. – TomTom Jul 09 '12 at 11:22
  • ok, I mean read in some function, when I will need this "." for some calculations. Oh, and last question. Is this SQL query will works properly? `select * from tableXYZ where lat >='" + latUp + "'"`, where "latUp" is doubletype (from c#), and lat from tableXYZ is float type? greetings! – whoah Jul 09 '12 at 11:28
  • No, calculations work without ".", They work on the binary value. And SQL should use parameters. – TomTom Jul 09 '12 at 11:55
0

As Damien_The_Unbeliever stated formatting is (well should be irrelevant) formatting is something you do for display, reporting etc.

As for whether to use floating point or fixed point (decimal), decimal solves a lot of issues IF the language you are using to access it has a decimal type. If you are manipulating the numbers as doubles then using decimal on the back end won't give you that much, as you will still be manually coping with the inherent inaccuracies of floating point representation.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • okay, thanks. So now is time for question as stupid as hell -> Can I send double-type (point) into column with float type? If not, what should I do, to send this value `52.22105994970536` into database? Or maybe this solution is better? `float x = (float)52.22105994970536;` – whoah Jul 09 '12 at 11:10
  • By default, FLOAT in Sql Server has the same width in bytes as a double from the programming languages you're familiar with (even though the example you wrote has way too many significant digits.) – shawnt00 Jul 09 '12 at 15:05