Reputation: 705
Why does the below code throw a System.FormatException
-
Input string was not in a correct format
decimal sum = 0;
string valString = "3.5";
sum += Convert.ToInt32(valString);
Upvotes: 0
Views: 203
Reputation: 186833
Well, 3.5
is not a correct integer value (please, notice fraction part - .5
), it's decimal
in the context:
decimal sum = 0;
string valString = "3.5";
sum += Convert.ToDecimal(valString);
Upvotes: 4