Search code examples
c#stringvisual-studiodoubleloaddata

How to load data into program as double? (not string) in c# (window form application - visual studio)


I dont know how to ask, but i want to load data in double not in string as you can see in the code. Can i change them in double? i want to store them in array because i want to find maximum value from the data. cannot find them in string.

private void button1_openfile_Click(object sender, EventArgs e)
    {
        //load data from text file
        string[] lines = File.ReadAllLines(@"C:\Users\Siti Nurhazwani\Desktop\table.txt");
        string[] values;

        for (int i = 0; i < lines.Length; i++)
        {
            values = lines[i].ToString().Split('/');
            string[] row = new string[values.Length];

            for (int j = 0; j < values.Length; j++)
            {
                row[j] = values[j];
            }

            table.Rows.Add(row);
        }
        

    }

can someone share code to me how to change the string to double? please use simple terms i am a beginner in c#.


Solution

  • Change these lines in your code to double.

    string[] row = new string[values.Length];
    
    for (int j = 0; j < values.Length; j++)
    {
        row[j] = values[j];
    }
    

    Instead

    double[] row = new double[values.Length];
    
    for (int j = 0; j < values.Length; j++)
    {
       row[j] = Convert.ToDouble(values[j]);
    }