Convert “1.79769313486232E+308” to double without OverflowException?

后端 未结 8 1334
暗喜
暗喜 2020-12-07 01:20

I have this string \"1.79769313486232E+308\" and am trying to convert it to a .NET numeric value (double?) but am getting the below exception. I am using Convert.ToDo

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 02:00

    Demonstrates the issue and a solution:

    var s = double.MaxValue.ToString();
    double d;
    if (!double.TryParse(s, out d)) {
        d = s.Equals(double.MaxValue) ? double.MaxValue : double.MinValue;
    }
    

提交回复
热议问题