转换" 1.79769313486232E + 308"加倍没有发生OverflowException?发生、QUOT、OverflowException

2023-09-03 05:59:07 作者:侵她国做她王

我有这样的字符串1.79769313486232E + 308,我试图将其转换为一个.NET数值(双?),但我得到下面的异常。我使用 Convert.ToDouble()。什么是正确的方式做到这一点的转换?

  

发生OverflowException:值要么过大或过小的双

解决方案

该问题可能是由于这样的事实: Double.MaxValue 被转换为字符串,而当该字符串是输出,而不是所有的数字被输出,相反,它是圆形的。解析这个值溢出了翻番。

使用 Double.TryParse 键,然后在出现故障时检查的字符串1.79769313486232E + 308平等和代 Double.MaxValue 应该是一个快速的解决方法,如果你需要保持串事情是这样的。

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.ToDouble(). What is the proper way to do this conversion?

OverflowException: Value was either too large or too small for a Double

多项目中指定项目金额求和函数解法

解决方案

The problem is likely due to the fact that Double.MaxValue was converted to a string, and when the string is output, not all the digits are output, instead it is rounded. Parsing this value overflows the double.

Using Double.TryParse and subsequently checking equality on the string "1.79769313486232E+308" in case of failure and substituting Double.MaxValue should be a quick workaround, if you need to keep the string the way it is.