-2
Console.Write("First Number: ");
double n1 = double.Parse(Console.ReadLine());
Console.Write("Second Number: ");
double n2 = double.Parse(Console.ReadLine());
double r = n1+n2;
Console.WriteLine($"The result r are {r}");

When I input, for example: 2.3 and 5.2, the output is that r equals to 23 + 52 = 75, and not 7.5. Why?

  • See duplicate for how to parse if you want to always require a period as the decimal separator. That said, you should consider the wisdom, or lack thereof, of implementing a program that ignores the user's current locale settings. – Peter Duniho Feb 21 '21 at 00:38

1 Answers1

0

What version of .NET framework are you using? Example you gave works fine when run on .NET 5.

What crossed my mind is that the runtime takes localization into account and doesn't look for . as decimal point and ignores it instead. Maybe it looks for ,.

What you can try is passing some other localization setting to a parser like so

double.TryParse(Console.ReadLine(), NumberStyles.Number, CultureInfo.CreateSpecificCulture ("en-US"), out temp)

This should work with ..

Dacha
  • 161
  • 1
  • 4
  • You can force other localization for your application like this: Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); This will use US localization. – Dacha Feb 21 '21 at 00:40