什么代表sql server中的double? [英] What represents a double in sql server?

查看:60
本文介绍了什么代表sql server中的double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中有几个属性是 double,我想将它们存储在 SQL Server 的表中,但注意到没有 double 类型,那么最好使用什么,decimal 还是 float?

这将存储纬度和经度值,因此我需要最准确的精度.

感谢到目前为止的回复.

解决方案

float

或者,如果你想上学:

真实

您也可以使用 float(53),但它的含义与 float 相同.

("real" 等价于 float(24),而不是 float/float(53).)

decimal(x,y) SQL Server 类型适用于您需要精确 十进制数而不是浮点数(可以是近似值)的情况.这与 C# 的十进制"数据类型形成对比,后者更像是一个 128 位浮点数.

MSSQL 的 float 类型相当于 .NET 中的 64 位 double 类型.(我 2011 年的原始回答说尾数可能略有不同,但我在 2020 年对此进行了测试,它们在非常小和非常大的数字的二进制表示中似乎 100% 兼容——请参阅 https://dotnetfiddle.net/wLX5Ox 用于我的测试.

为了让事情变得更混乱,C# 中的float"只有 32 位,因此它在 SQL 中与 MSSQL 中的 real/float(24) 类型比在 float/float(53) 中更等价.

在您的特定用例中...您只需要在小数点后5位表示大约一米精度的纬度和经度,而度数只需要小数点前三位.Float(24) 或 decimal(8,5) 最适合您在 MSSQL 中的需求,并且在 C# 中使用 float 就足够了,您不需要 double.事实上,您的用户可能会感谢您四舍五入到小数点后 5 位而不是一堆无关紧要的数字.

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.

解决方案

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's float type is equivalent to the 64-bit double type in .NET. (My original answer from 2011 said there could be a slight difference in mantissa, but I've tested this in 2020 and they appear to be 100% compatible in their binary representation of both very small and very large numbers -- see https://dotnetfiddle.net/wLX5Ox for my test).

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.

这篇关于什么代表sql server中的double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆