Home > Software engineering >  How to convert/parse string array to double?
How to convert/parse string array to double?

Time:06-11

I have a string array with text and numbers. I extracted a number from it but I can't get it into an int or double variable to use later for comparing or anything.

Instead, I used Compare method which is not working like I wanted to, to output bigger number from those 2 strings. What I want is to just put the number in variable or have that compare method work. Thank you I am a beginner.

string[] Months = new string[12];
            Months[0] = "Január 1"; //1
            Months[1] = "Február 0"; //2
            Months[2] = "Marec 4,5"; //3
            Months[3] = "Apríl 8.3";  //4
            Months[4] = "Máj 13";   //5
            Months[5] = "Jún 17.3";  //6
            Months[6] = "Júl 20.5";     //7
            Months[7] = "August 21";    //8
            Months[8] = "September 15"; //9
            Months[9] = "October 8";    //10   
            Months[10] = "November 4";   //11
            Months[11] = "December 1.4";   //12

double result = 0;
double result2 = double.Parse(Months[5].Substring(Months[5].IndexOf(" ")).TrimStart());
                
//double.TryParse(double.Parse(Months[5].Substring(Months[5].IndexOf(" ")).TrimStart()), out result);
Console.WriteLine(result2);


 if (string.Compare(Months[4].Substring(Months[4].IndexOf(" ")).TrimStart(), 
Months[5].Substring(Months[5].IndexOf(" ")).TrimStart()) > 0)
{
//Console.WriteLine("1");
Console.WriteLine(Months[4].Substring(Months[4].IndexOf(" ")).TrimStart());
}

CodePudding user response:

If you just want the numeric part of each, in an array:

var nums = Months.Select(m => double.Parse(m.Split()[1]));  

The Split() without a parameter splits the string into an array, using any whitespace as the separator. We then select the second entry in the split array to access just the numeric part, and pass that to double.Parse() to parse each sub-string as a double.

The Months.Select() is a linq function, and will require adding a using for System.Linq if you want to learn more, I'd suggest learning more about how linq works.

CodePudding user response:

You can use double.Parse(yourString, CultureInfo.InvariantCulture) to parse your strings, if you split them into parts first by the space in the strings.

string[] Months = new string[12];
Months[0] = "Január 1"; //1
Months[1] = "Február 0"; //2
Months[2] = "Marec 4,5"; //3
Months[3] = "Apríl 8.3";  //4
Months[4] = "Máj 13";   //5
Months[5] = "Jún 17.3";  //6
Months[6] = "Júl 20.5";     //7
Months[7] = "August 21";    //8
Months[8] = "September 15"; //9
Months[9] = "October 8";    //10   
Months[10] = "November 4";   //11
Months[11] = "December 1.4";   //12

var parts = Months[5].Split(" ");

double result2 = double.Parse(parts[1], CultureInfo.InvariantCulture);

Console.WriteLine(result2); // Prints 17,3

var month4Parts = Months[4].Split(" ");
var month5Parts = Months[5].Split(" ");

var month4AsDouble = double.Parse(month4Parts[1], CultureInfo.InvariantCulture);
var month5AsDouble = double.Parse(month5Parts[1], CultureInfo.InvariantCulture);
if (month4AsDouble < month5AsDouble) // If 13 < 17,3
{
    Console.WriteLine(month5AsDouble);
}
  • Related