简体   繁体   English

C#decimal.Parse()奇怪的FormatException

[英]C# decimal.Parse() strange FormatException

There is MyDataSet myDataSet with MyTable with column string MyNumber (names changed). MyDataSet myDataSet带有MyTable ,列string MyNumber (名称已更改)。 Then a row is added to the table (existing code): 然后在表中添加一行(现有代码):

decimal d = GetSum();
myDataSet.MyTable.AddMyTableRow(d.ToString("F2"));

Now I have to get that decimal number. 现在我必须得到那个十进制数。 But it throws FormatException when I try 但是当我尝试时它会抛出FormatException

decimal.Parse(myDataSet.MyTable[0].MyNumber);
// or
decimal.Parse(myDataSet.MyTable[0].MyNumber, CultureInfo.InvariantCulture);
//or
decimal.Parse(myDataSet.MyTable[0].MyNumber, CultureInfo.CurrentCulture)
//or
anything else..

I had a similar issue a few days back. 几天前我有类似的问题。 This however was with my ModelBinding in a MVC project. 然而,这是我在MVC项目中的ModelBinding。 I had to overwrite the class responsible for binding decimal values to model variables. 我不得不覆盖负责将十进制值绑定到模型变量的类。

Try to replace the full stop (.) with a comma(,) 尝试用逗号(,)替换句号(。)

I would guess that a FormatException suggests that you are not putting a string value into the Decimal.Parse method. 我猜想FormatException建议您不要将字符串值放入Decimal.Parse方法。

http://msdn.microsoft.com/en-us/library/cafs243z.aspx http://msdn.microsoft.com/en-us/library/cafs243z.aspx

The MSDN suggests that: MSDN建议:

FormatException = s is not in the correct format. FormatException = s的格式不正确。

From the looks you are accessing the column itself rather than the data contained in it. 从您查看列的内容而不是其中包含的数据。

我通过为这些数字创建额外的十进制列来解决这个问题

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

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