测试SqlDecimal和C#十进制之间的差异 [英] Testing differences between SqlDecimal and C# Decimal
问题描述
我正在尝试验证c#中的小数是否适合数据库列的小数。 SqlDecimal对象允许您将精度和小数位数以及十进制位传递给构造函数。我知道列的大小,因此在写数据之前,我会检查每个输入,以便产生业务所需的输出。
I'm trying to validate that a decimal in c# will fit into a db column decimal. The SqlDecimal object allows you to pass in a precision and scale as well as the decimal bits to the constructor. I know the size of the column and so before we write the data I check each input so I can produce business required output.
在这种情况下,我们存储一个百分比,因此精度为 13 ,比例尺为 10 。我有一个测试工具,下面将其简化为SO的统一测试。此示例在SqlDecimal构造函数行上引发算术溢出错误:
In this case we are storing a percent, so the precision is 13 and the scale is 10. I have a testing harness that I've condensed below into a unti test for SO. This sample is throwing an Arithmetic Overflow error on the SqlDecimal constructor line:
[TestMethod]
public void TestDecimalFits()
{
decimal d = 10.3m;
SqlDecimal sqlDecimal = new SqlDecimal(13, 10, d >= 0, Decimal.GetBits(d));
Assert.AreEqual(d, sqlDecimal.Value);
}
有人知道为什么会爆炸吗?
Does anyone know why this blows up?
谢谢!
推荐答案
Decimal.GetBits $ c的返回$ c>与
SqlDecimal
的构造函数参数不兼容。
The return of Decimal.GetBits
is not compatible with the SqlDecimal
's constructor parameter.
Decimal.GetBits
返回一个数组,该数组代表小数的确切结构,其中包括96位缩放整数值和8位指数(加上1个符号位和27个未使用位)。
Decimal.GetBits
returns an array that represents the exact structure of the decimal, which includes the 96-bit scaled integer value and 8 bits for the exponent (plus 1 sign bit and 27 unused bits).
SqlDecimal
构造函数所用的 int
数组,它表示提供新SqlDecimal值的128位无符号整数。 -不是该十进制值的表示。 scale
参数确定小数的实际值是什么。
The SqlDecimal
constructor that you are using takes an int
array that represents "The 128-bit unsigned integer that provides the value of the new SqlDecimal." - NOT the represenatation of that decimal value. The scale
parameter determines what the actual value of the decimal will be.
因此您实际上传递的值不同于构造函数期望的值。 .NET十进制等效项为10.3m是
So you are effectively passing in a different value than the constructor expects. The .NET decimal equivalent of 10.3m is
0000000001100111-0000000000000000-0000000000000000-10000000000000000
其中 1100111
是103的二进制等效项,而 1
Where 1100111
is the binary equivalent of 103, and 1
is the scale.
该二进制值的等价整数大于13位,这就是为什么将其传递给它时会溢出的原因。
The integer equivalent of that binary value is more than 13 digits long, which is why you get an overflow when passing it to the SqlDecimal constructor.
我不会玩弄位弄乱,而只是使用原始的十进制
值,让SQL将其转换为正确的精度并自动缩放。
I would not play around with bit-fiddling and instead just use the raw decimal
value, letting SQL convert it to the right precision and scale automatically.
我正在尝试验证c#中的小数是否适合db列小数。
I'm trying to validate that a decimal in c# will fit into a db column decimal.
那么, DECIMAL(13,10)
的最大值是 999.9999999999
,该值远低于十进制
的最大值。所以不,您不能在 DECIMAL(13,10)
SQL列中存储任何 C#十进制值。
Well, the largest value that will fit into a DECIMAL(13,10)
is 999.9999999999
, which is well below the maximum value of a decimal
. So no, you can't store any C# decimal value in a DECIMAL(13,10)
SQL column.
(从技术上讲,我认为您可以通过降低精度来存储 9999999999999
,但即使这样也远低于十进制
)。
(technically I think you can store 9999999999999
by dropping the precision, but even that is well below the maximum value of a decimal
).
这篇关于测试SqlDecimal和C#十进制之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!