Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string to float?

Tags:

c#

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?

like image 925
Bramble Avatar asked Dec 04 '09 02:12

Bramble


2 Answers

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

like image 69
Matt Avatar answered Oct 20 '22 01:10

Matt


MyArray[i, j] = Convert.ToSingle(sites[j]);
like image 32
SoapBox Avatar answered Oct 20 '22 01:10

SoapBox