无法根据 IEEE 754 在 C# 中将字符串值转换为浮点数

Not able to convert string value to float in C# according to IEEE 754

嗯,我有一个字符串值 "1.36383E+37",我想根据 IEEE 754 转换为浮点类型。请参见下面的代码:

        try
        {
            return float.Parse(val, NumberStyles.Float | NumberStyles.AllowThousands, cultureInfo);
        }
        catch (Exception ex)
        {
        }

无法解析值并抛出异常 "Value was either too large or too small for a Single"

如有帮助,将不胜感激。

我怀疑您正在使用 cultureInfo 作为使用 . 作为千位分隔符的文化。例如,CultureInfo.GetCultureInfo("it")(意大利语)导致您为该数据描述的错误,其他几个也是如此。您应该使用与数字书写方式相匹配的文化。对于机器生成的字符串,CultureInfo.InvariantCulture 将是最佳选择。