Reputation: 2984
I'm currently trying to convert a few numbers from a DB2 Server into double values in C#.
The getting of the data from the DB2 Server is not a Problem, and I get the data into a DataTable quite easily. The Problem then Comes when I try to convert the objects into double values, as the Notation is different (, instead of . as example).
Thus the code:
foreach (DataRow row in DataTable myResultTable)
{
double myValue = String.IsNullOrEmpty(row["myValue"].ToString())? 0 : (double)row["myValue"]; // myValue has 1234,56 as Content.
}
Fails with an exception that the value can't be converted.
The datatype of the field myValue in the db2 is Decimal with a length of 16.
As I didn't find anything, I thought about converting it to string, formating it there and then transform that into a double but that seems quite....complicated to me for something that should be easy (and complicated always means prone to Errors because of something unexpected).
So my question is: Is there any easy way to do this Transformation?
Edit:
As it was asked a GetType on row["myValue"] results in: {Name = "Decimal" FullName = "System.Decimal"}
.
Upvotes: 0
Views: 59
Reputation: 174329
The real solution is to cast to decimal
not to double
:
var value = row["myValue"] is DBNull ? 0m : (decimal)row["myValue"];
Upvotes: 2