9

I have this bit of code here:

int i = 0;

        StreamReader re = File.OpenText("TextFile1.txt");
        string input = null;

        while ((input = re.ReadLine()) != null)
        {
            string[] sites = input.Split(' ');
            for (int j = 0; j < sites.Length; j++)
            {
                MyArray[i, j] = Convert.ToInt32(sites[j]);
            }
            i++;
        }


     for (int a = 0; a < 5; a++)
     {
            for (int j = 0; j < 5; j++)
            {
                Console.Write(MyArray[a, j] + " ");

            }
            Console.WriteLine();
     }

My problem is this line of code

MyArray[i, j] = Convert.ToInt32(sites[j]);

Its getting converted to an int, how do I convert it to a float?

Bramble
  • 1,375
  • 10
  • 38
  • 55

3 Answers3

33

Try float.Parse(string) or Double.Parse(string)

Matt
  • 24,106
  • 61
  • 180
  • 291
9
MyArray[i, j] = Convert.ToSingle(sites[j]);
SoapBox
  • 19,716
  • 3
  • 46
  • 86
5

Convert.ToSingle method or whole bunch of others.

EDIT:
Here's an related article: Double.TryParse or Double.Convert - what is faster and more safe? of interest in SO.

Community
  • 1
  • 1
o.k.w
  • 24,261
  • 6
  • 60
  • 62