How to convert Console.ReadLine() to double in simple calculator ? C# Problem with exception

前端 未结 1 355
日久生厌
日久生厌 2021-01-28 05:16

I used double.Parse and Convert.ToDouble and got the same error

System.FormatException: Input string was not in a correct format

相关标签:
1条回答
  • 2021-01-28 05:46

    The problem might be your culutral setting. Some countries uses a . others uses a ,

    try:

    double num2 = double.Parse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture);
    

    or set the separator like:

    var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    culture.NumberFormat.NumberDecimalSeparator = ".";
    double num2 = DateTime.Parse(Console.ReadLine(), culture);
    
    0 讨论(0)
提交回复
热议问题