0

I've tried the code below but it does not work properly. It basically asks user to enter 2 decimal numbers and adds them up. But there is a little problem with it. For example when I enter 1.3 as the first and 2.5 as the second number, the result is 38 instead of 3.8.

Console.Write("Enter a number: ");
double num3 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter another number: ");
double num4 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine(num3 + num4);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 1
    what is your locale? in some cultures, `.` means "group separator", so `1.3` is thirteen; and `1,3` is one point three; if you're in such a locale, and you want to use a different culture for parsing: you'll need to specify the culture explicitly in `ToDouble` (presumably `CultureInfo.InvariantCulture` or `NumberFormatInfo.InvariantInfo`) – Marc Gravell Apr 07 '20 at 10:58
  • Hi Mice, you can use `Decimal.Add(num3, num4)` – sisindrymedagam Apr 07 '20 at 11:03
  • @SisindryMedagam that isn't the problem; a: OP isn't using actually `decimal`, and b: the `+` operator works just fine for both `decimal` and `double` – Marc Gravell Apr 07 '20 at 11:31
  • @MarcGravell Thank you sir. As you suggested I tried "," instead of "." and it worked. – Mice From Kæhta Apr 07 '20 at 13:32

0 Answers0