什么代表sql server中的double?

探讨了在SQL Server中存储C#中的Double类型数据的最佳实践。比较了使用Float和Decimal类型的优劣,特别是在存储纬度和经度值时,强调了精度和效率之间的平衡。
部署运行你感兴趣的模型镜像

本文翻译自:What represents a double in sql server?

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

This will store latitude and longitude values, so I need the most accurate precision. 这将存储纬度和经度值,因此我需要最精确的精度。

Thanks for the responses so far. 感谢到目前为止的回复。


#1楼

参考:https://stackoom.com/question/54Yv/什么代表sql-server中的double


#2楼

float

Or if you want to go old-school: 或者如果你想去老派:

real

You can also use float(53), but it means the same thing as float. 你也可以使用float(53),但它和float一样。

("real" is equivalent to float(24), not float/float(53).) (“real”相当于float(24),而不是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). 十进制(x,y) SQL Server类型适用于需要精确十进制数而不是浮点数(可以是近似值)的情况。 This is in contrast to the C# "decimal" data type, which is more like a 128-bit floating point number. 这与C#“十进制”数据类型形成对比,后者更像是一个128位浮点数。

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. MSSQL float与.NET中的64位double类型没有完全相同的精度(尾数IIRC略有不同),但它与大多数用途相当接近。

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). 为了让事情更加混乱,C#中的“float”只有32位,因此在SQL中,它与MSSQL中的real / float(24)类型相比,更接近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. 在您的特定用例中...您需要的只是小数点后面的5个位置来表示大约一米精度内的纬度和经度,并且您只需要在小数点前最多三位数。 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. Float(24)或十进制(8,5)最适合您在MSSQL中的需求,并且在C#中使用float是足够好的,您不需要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. 实际上,您的用户可能会感谢您舍入到小数点后5位,而不是为了骑行而出现一堆无关紧要的数字。


#3楼

For SQL Sever: 对于SQL Sever:

Decimal Type is 128 bit signed number Float is a 64 bit signed number. 十进制类型是128位有符号数字Float是64位有符号数字。

The real answer is Float , I was incorrect about decimal. 真正的答案是Float ,我对十进制不正确。

The reason is if you use a decimal you will never fill 64 bit of the decimal type. 原因是如果使用小数,则永远不会填充64位的十进制类型。

Although decimal won't give you an error if you try to use a int type. 虽然如果尝试使用int类型,decimal不会给出错误。

Here is a nice reference chart of the types. 是一个很好的参考图表类型。


#4楼

你应该将它映射到FLOAT(53) - 这就是LINQ to SQL的作用


#5楼

float in SQL Server actually has [edit:almost] the precision of a "double" (in a C# sense). SQL Server中的float实际上有[编辑:几乎]精度为“double”(在C#意义上)。

float is a synonym for float(53) . floatfloat(53)的同义词。 53 is the bits of the mantissa. 53是尾数的位。

.NET double uses 54 bits for the mantissa. .NET double使用54位作为尾数。


#6楼

float is the closest equivalent. float是最接近的等价物。

SqlDbType Enumeration SqlDbType枚举

For Lat/Long as OP mentioned. 对于Lat / Long,如OP所述。

A metre is 1/40,000,000 of the latitude, 1 second is around 30 metres. 一米是纬度的1 / 40,000,000,1秒是30米左右。 Float/double give you 15 significant figures. Float / double给你15个有效数字。 With some quick and dodgy mental arithmetic... the rounding/approximation errors would be the about the length of this fill stop -> "." 有一些快速和狡猾的心算...圆角/近似误差将是这个填充停止的长度 - >“。”

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

<think>好的,用户想了解SQL Server中的double数据类型的使用方法和示例。首先,我需要确认在SQL Server中是否存在名为double的数据类型。根据已有的知识,SQL Server中的浮点类型主要是float和real,其中float对应的是双精度,而real是单精度。所以,可能用户提到的double实际上对应的是float类型。 接下来,我需要查阅引用的资料。引用[1]提到在创建表时必须为列指派数据类型,但没有直接提到double。引用[3]讨论了money和decimal的选择,但同样不涉及double。不过,根据SQL Server的官方文档,float(n)可以存储浮点数,当n在1-24时为单精度(real),25-53时为双精度(double precision)。因此,用户可能需要使用float来替代double。 接下来,用户需要基本用法和应用场景。基本用法包括创建表时指定float类型,插入数据时的注意事项,比如精度问题。示例部分应该展示如何创建表、插入数据以及查询。应用场景可能涉及科学计算、大数据量的数值处理等,但要注意float的近似值特性,不适合需要精确计算的场景,这时候应该用decimal。 另外,需要注意用户可能存在的误区,比如直接使用double类型,而实际上应该用float。需要明确说明SQL Server中没有double类型,但float可以达到双精度。同时,要提醒用户关于精度丢失的问题,避免在需要精确存储时使用float,而应选择decimal。 最后,生成相关问题,帮助用户进一步了解相关数据类型的选择和转换,比如与decimal的比较,如何转换类型等。</think>在 SQL Server 中,没有直接名为 `DOUBLE` 的数据类型,但可以通过 `FLOAT` 数据类型实现双精度浮点数存储。以下是具体说明: --- ### 一、`FLOAT` 数据类型的基本用法 1. **定义方式** `FLOAT(n)` 表示浮点数,其中 `n` 为精度位数: - `n=1-24`:单精度浮点数(实际对应 `REAL` 类型,存储空间 4 字节) - `n=25-53`:双精度浮点数(默认 `n=53`,存储空间 8 字节) ```sql -- 创建表时定义双精度列 CREATE TABLE Measurements ( SensorID INT PRIMARY KEY, Value FLOAT(53) -- 双精度浮点数 ); ``` 2. **插入数据** 直接插入数值即可,支持科学计数法: ```sql INSERT INTO Measurements (SensorID, Value) VALUES (1, 1234.5678), (2, -5.2E-3); -- 科学计数法表示 -0.0052 ``` 3. **精度特性** `FLOAT` 是近似数值类型,适合处理极大或极小数,但可能存在微小舍入误差: ```sql SELECT 0.1 + 0.2; -- 输出可能为 0.30000000000000004(近似值) ``` --- ### 二、应用场景 1. **科学计算** 适合物理模拟、工程计算等需要处理极大/极小值的场景,例如: ```sql CREATE TABLE AstronomicalData ( ObjectID INT, DistanceFromEarth FLOAT(53) -- 存储以光年为单位的天体距离 ); ``` 2. **大数据量数值处理** 在数据分析中处理传感器读数、温度记录等非精确需求的数据: ```sql CREATE TABLE SensorLogs ( LogID INT IDENTITY, Timestamp DATETIME, Temperature FLOAT(53) -- 温度值允许微小误差 ); ``` 3. **计算密集型操作** 使用 `FLOAT` 进行复杂数学运算(如平方根、对数)时效率较高: ```sql SELECT SQRT(Value) FROM Measurements; -- 平方根计算 ``` --- ### 三、注意事项 1. **避免精确计算** 金融、货币等需要精确值的场景应改用 `DECIMAL` 或 `NUMERIC` 类型[^3]。 2. **显式类型转换** 与字符串或其他类型交互时需用 `CAST` 或 `CONVERT`: ```sql SELECT CAST(Value AS DECIMAL(10,2)) FROM Measurements; -- 转换为精确数值[^2] ``` 3. **比较操作的误差处理** 比较浮点数时需设置误差范围: ```sql SELECT * FROM Measurements WHERE ABS(Value - 3.14159) < 1e-6; -- 允许微小误差 ``` --- ### 四、示例代码 ```sql -- 创建双精度浮点数列 CREATE TABLE ScientificResults ( ExperimentID INT PRIMARY KEY, Result FLOAT(53), Uncertainty FLOAT(53) ); -- 插入实验数据 INSERT INTO ScientificResults (ExperimentID, Result, Uncertainty) VALUES (101, 6.02214076E23, 0.00000003E23), -- 阿伏伽德罗常数 (102, 9.80665, 0.00001); -- 重力加速度 -- 查询结果范围 SELECT * FROM ScientificResults WHERE Result BETWEEN 1E20 AND 1E25; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值