Input string was not in a correct format #2

后端 未结 10 1225
一向
一向 2020-12-03 21:24
double temp;
temp = (double)Convert.ToDouble(\"1234.5678\");

Hey Lads and Ladies, I can\'t for the life of me figure out why the above line isn\'t

相关标签:
10条回答
  • 2020-12-03 22:00

    Check your regional settings. Your decimal symbol needs to be ".".

    0 讨论(0)
  • 2020-12-03 22:01

    You may be somehow using a european local. In some countries the . and , in numbers is reversed.

    0 讨论(0)
  • 2020-12-03 22:04

    Hi as Mario says you must parse it taking into account the regional settings.

    double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
    

    Regards.

    0 讨论(0)
  • 2020-12-03 22:06

    I recommend you use TryParse instead, so you don't need to handle parsing exceptions.

    double temp = 0;
    if (double.TryParse("123.456", out temp)
    {
        Console.WriteLine(string.Format("Parsed temp: {0}", temp);
    }
    else
    {
        Console.WriteLine("Input value was not able to be parsed.");
    }
    
    0 讨论(0)
提交回复
热议问题