简体   繁体   English

C# 中 SQL 服务器的数字的等效数据类型是什么

[英]What is the equivalent datatype of SQL Server's Numeric in C#

In SQL Server we can write data AS Numeric(15,10) .. what will the equivalent of this in C#?在 SQL 服务器中,我们可以将数据写入为Numeric(15,10) .. 在 C# 中相当于什么?

I know that Numeric 's equivalent is Decimal but how to represent Numeric(15,10) ?我知道Numeric的等价物是Decimal但如何表示Numeric(15,10)

There isn't a direct equivalent, in that there are no built-in .NET types which allow you to specify the precision/scale explicitly as far as I'm aware.没有直接的等价物,因为据我所知,没有内置的 .NET 类型允许您明确指定精度/比例。 There's no fixed -point type like NUMERIC.没有像 NUMERIC 这样的定点类型。

decimal and double are the common floating point types in .NET, with decimal implementing decimal floating point (like NUMERIC in T-SQL) and double implementing binary floating point behaviour (like FLOAT and REAL in T-SQL). decimaldouble是 .NET 中常见的浮点类型,其中decimal实现十进制浮点(如 T-SQL 中的 NUMERIC)和double实现二进制浮点行为(如 T-SQL 中的 FLOAT 和 REAL)。 (There's float as well, which is a smaller binary floating point type.) (还有float ,它是一种较小的二进制浮点类型。)

You should choose between decimal and double based on what values you're going to represent - I typically think of "man-made", artificial values (particularly money) as being appropriate for decimal , and continuous, natural values (such as physical dimensions) as being appropriate for double .您应该根据要表示的值在decimaldouble精度之间进行选择-我通常认为“人造”、人工值(尤其是金钱)适合decimal和连续的自然值(例如物理尺寸) ) 适合double

Try looking at this site as a guide to the data type mappings.尝试查看此站点作为数据类型映射的指南。 As far as the precision and length, you control that yourself using format specifiers至于精度和长度,您可以使用格式说明符自行控制

There are two answers depending on two questions:根据两个问题,有两个答案:

1) What is something that allows you to specify the precision and scale. 1)什么东西可以让你指定精度和比例。 Nothing.没有什么。 This seems like your question, but just in case:这似乎是您的问题,但以防万一:

2) What is something that allows you to specify a decimal floating point number exactly . 2)什么东西可以让你精确地指定一个十进制浮点数。 This is indeed the Decimal type -- but the point is internal and is set to one of 2^32 positions based on the input number.这确实是 Decimal 类型——但该点是内部的,并且根据输入数字设置为 2^32 个位置之一。 Not sure why, but only 28 values work, or 2^5 - 4..不知道为什么,但只有 28 个值有效,或者 2^5 - 4..

So even though.Net allows the Decimal to look like a float, it is very different under the covers and does match the Decimal of SQLServer.因此,尽管.Net 允许 Decimal 看起来像一个浮点数,但实际上它是非常不同的,并且与 SQLServer 的 Decimal 匹配。 Anything not a sum of distinct power of 2 values is an estimation using the normal binary floating point.任何不是 2 个值的不同幂的总和都是使用正常二进制浮点的估计。 This means even something such as the number 0.1, has already lost precision.这意味着即使是数字 0.1,也已经失去了精度。 But not with the Decimal type.但不是 Decimal 类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM