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?

1
  • float[,] MyArray = new float[5, 5]; MyArray[i, j] = Convert.ToSingle(sites[j]); Commented Dec 4, 2009 at 2:37

3 Answers 3

33

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

1 Comment

With Convert.ToDouble, I was expecting there to be a Convert.ToFloat. This makes sense though.

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.