rock
rock

Reputation: 705

Error Converting String to Integer

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

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

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

AlMudy
AlMudy

Reputation: 11

Please try this:

sum += System.Convert.ToDecimal(valString);

Upvotes: 1

Related Questions