C# Converting a string containing a floating point to an integer

Viewed 32955

What is the best way to take a string which can be empty or contain "1.2" for example, and convert it to an integer? int.TryParse fails, of course, and I don't want to use float.TryParse and then convert to int.

7 Answers

I had this same problem and ended up using a hybrid of Mark's and Dariusz':

 if (num == "")
     {
      num = "0.00";
     }

  var num1 = (float)Convert.ToDouble(num);
Related