Input String was not in a correct format error from Decimal.Parse when precision is greater than 4

Input String was not in a correct format error from Decimal.Parse when precision is greater than 4

我在尝试使用 Decimal.Parse 转换小数精度为 4 或更高的 C# 时遇到问题。它抛出错误:

Input string was not in a correct format" when it tries to execute the function with the value 0.00006 or greater precision.

我已经尝试使用 InvariantCulture 并排除了这个问题,因为这是我看到的建议其他人遇到此错误的方法。

我也尝试过使用decimal.Parse 无济于事。

正在执行的代码行是:

Decimal decVal = Decimal.Parse(Value.ToString())

其中值是字符串“0.00006”。

其他人以前遇到过这个问题吗?这是 Decimal.Parse 函数固有的问题,还是我正在做的事情?

我明白了。事实证明,当精度超过 4 位时,ToString 返回科学记数法,因此修复是使用以下代码:

Decimal decVal = Decimal.Parse(Value.ToString(), NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint)

通过阅读这个问题得出结论: How to convert this scientific notation to decimal?

感谢所有提供意见的人!