Converter 1.0 (string) to 1.0 (double) in C #

-1

Good evening guys,

I'm trying to solve a challenge in C # but when I try to convert two variant strings with dot (Ex. 1.0 7.0) to two double variables, it loses the point and gets 10 70. Can anyone tell me what I'm doing wrong? / p>

double x1 , x2;
Console.WriteLine();
value = Console.ReadLine();
String[] substrings = value.Split(delimiter);

x1 = Convert.ToDouble(substrings[0]);
x2 = Convert.ToDouble(substrings[1]);
    
asked by anonymous 22.07.2018 / 02:23

1 answer

-1

This is because Convert.ToDouble uses the culture parameters defined in Windows settings.

If you really want the point to be considered a decimal point and not a thousand (as it should be defined on your computer) you have to use the following structure:

Convert.ToDouble ( String, IFormatProvider)

Example:

double x1 , x2;
Console.WriteLine();
value = Console.ReadLine();
String[] substrings = value.Split(delimiter);

x1 = Convert.ToDouble(substrings[0],new CultureInfo("en-US"));
x2 = Convert.ToDouble(substrings[1],new CultureInfo("en-US"));

See how it works in .NET Fiddle

    
22.07.2018 / 02:38