Convert 1.0 (string) to 1.0 (double) in c#

Asked

Viewed 292 times

-1

Good evening, everyone,

I am trying to solve a challenge in C# but when I try to convert two variable string with point (Ex. 1.0 7.0) to two double variables, loses the point and is 10 70. Can someone tell me what I’m doing wrong?

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

x1 = Convert.ToDouble(substrings[0]);
x2 = Convert.ToDouble(substrings[1]);

1 answer

-1


This is because Convert.Todouble uses the crop parameters set in Windows settings.

If you really want the point to be considered decimal separator and not thousand (as it should be defined in your computer):

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 worked on .NET Fiddle

  • Thank you William, I actually swapped the point for a comma and it also worked. substrings[0] = substrings[0]. Replace(".", ","); But I really saw that there is this question and then I’ll test it that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.