0

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);

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

So dumb I didn't think that! I did do a ToDecimal but couldn't get to understand why ToInt didnt work. Thanks a ton for your answer!
1

Please try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.