I solved this by creating additional decimal columns for these numbers.
C# decimal.Parse() strange FormatException
-
28-06-2022 - |
質問
There is MyDataSet myDataSet
with MyTable
with column string MyNumber
(names changed). 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
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..
解決 3
他のヒント
I had a similar issue a few days back. This however was with my ModelBinding in a MVC project. 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.
http://msdn.microsoft.com/en-us/library/cafs243z.aspx
The MSDN suggests that:
FormatException = s is not in the correct format.
From the looks you are accessing the column itself rather than the data contained in it.