C#, Convert.ToInt32(string) question

  I've got some C# code in an inherited application that is not working. The problem seems to be that the input is a string consisting of a decimal number. The code is trying to convert the string to an integer, using the following line

   I did some searching and it seems that this method is expecting a whole number string, and won't work with a decimal number string. Is that correct? Is there an easy fix for this? I am expecting both whole numbers and decimal numbers as input here. Thanks for the help!

Who is Participating?
wdosanjosConnect With a Mentor Commented:
Yes, Convert.ToInt32(string) only works for integers.  Try Convert.ToDecimal(string) or Convert.ToDouble(string) if you expect decimal numbers.

More on the Convert class:
peetmConnect With a Mentor Commented:
You could ...

i = (Int32)Convert.ToDouble(your_string);
Daniel Van Der WerkenConnect With a Mentor Independent ConsultantCommented:
This is your best bet:

                    string a = "1.1";
                    decimal d = decimal.MinValue;
                    decimal.TryParse( a, out d );
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.