[英]Decimal out of range exception
In my C# application, I am trying to save a decimal price into a SQL Server table. 在我的C#应用程序中,我试图将十进制价格保存到SQL Server表中。 Columns type is
decimal
with no total digits defined. 列类型为
decimal
,未定义总数。
Without discount calculations, everything works fine. 没有折扣计算,一切正常。
But when I run some calculations, I get a final value of 21800
, and I get the following error when trying to save it. 但是当我运行一些计算时,我得到的最终值为
21800
,并且在尝试保存时出现以下错误。
"Parameter value '218000/00000000000000' is out of range."
I don't understand where the extra zeros come from! 我不明白额外的零来自哪里! The only thing I know is with
myValue.ToString()
I get 218000/00000000000000
, too! 我唯一知道的是
myValue.ToString()
我也得到了218000/00000000000000
!
I understand that the digits after floating point are caused by the calculations. 我知道浮点后的数字是由计算引起的。 But whatever they do, my value is
21800
when watching it. 但无论他们做什么,我看到它的价值是
21800
。 Why is it trying to save 218000/00000000000000
? 为什么要保存
218000/00000000000000
? Why does it care at all about the zeros? 为什么它关注零呢?
Any ideas why this happens and how to fix it? 任何想法为什么会发生这种情况以及如何解决它?
There are two ways to store decimal numbers: 存储十进制数的方法有两种:
Fixed-Point: This is what you have defined in your SQL Server project. 定点:这是您在SQL Server项目中定义的内容。 You defined
decimal (19,4)
, which means 19 digits total and 4 digits after the decimal point. 您定义了
decimal (19,4)
,表示总共19位和小数点后4位。 If you define such a type, then every number always has 4 digits after the decimal point; 如果定义这样的类型,则每个数字在小数点后总是有4位数; and also, you cannot store a number with more than 4 digits after the decimal point.
而且,您不能存储小数点后超过4位数的数字。 And note, there is no equivalent to fixed point in C#!
请注意,C#中没有等效的固定点!
Floating Point: These are the types float
, double
and decimal
in C#, and the type float(n)
in SQL Server. 浮点:这些是C#中的
float
, double
和decimal
类型,以及SQL Server中的float(n)
类型。 In floating point, as the names says, you can basically vary the number of digits before and behind the decimal point. 在浮点数中,正如名称所示,您基本上可以改变小数点前后的位数。 (Technically, you have a fixed number of digits, the so-called mantissa, and you can then move the decimal point around with a 2nd number, the exponent)
(从技术上讲,你有一个固定的位数,即所谓的尾数,然后你可以用第二个数字,指数移动小数点)
So now to your project: In your database, you use fixed point , while in C#, you have floating point . 所以现在对你的项目:在你的数据库中,你使用固定点 ,而在C#中,你有浮点 。 Therefore, when you calculate something in C#, and you want to store in to your database, then it is always converted from floating point to fixed point.
因此,当您在C#中计算某些内容并且想要存储到数据库中时,它始终会从浮点转换为固定点。 However, if your number in C# does not fit the decimal numbers in the database, then you get the out of range error.
但是,如果C#中的数字不符合数据库中的十进制数,则会出现超出范围的错误。
I had to specify the total digits of my decimal column in the SQL table (I set it as decimal (19, 4)
). 我必须在SQL表中指定我的十进制列的总位数(我将其设置为
decimal (19, 4)
)。
Besides, my C# representing field also needed to have the right precision count. 此外,我的C#表示字段也需要具有正确的精度计数。
I still don't know about the extra zeros and why SQL care about them. 我仍然不知道额外的零和SQL为何关心它们。 Any explanations is appreciated.
任何解释都表示赞赏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.