简体   繁体   中英

Adding 2 double numbers in c# (visual studio)

So, When I run the program and enter 2 numbers (eg 2.4 and 4.5), the result of adding is 69 and it isn't 6.9. But, when I run program and type 2,4 and 4,5 the result is 6,9?

Can anyone help me with this one. I am running this in Visual Studio 2019. The code is down below!

Code:

    static void Main(string[] args)
    {
        Console.Write("Enter a number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter a another number: ");
        double num2 = Convert.ToDouble(Console.ReadLine()); 
       
        Console.WriteLine(num1+num2);
        Console.ReadLine();
    }

Check your O/S region settings for numbers. In Windows 10: Control Panel > Region > Additional Settings. Look specifically at the Decimal symbol and Digit grouping symbol. Your code worked for me. When I changed the region settings on my machine (decimal symbol to a comma and Digit grouping symbol to a period), it worked as you described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM