我正在尝试将“$45.59”之类的字符串解析为小数。出于某种原因,我收到输入格式不正确的异常。我不关心所有本地化的东西,因为这不会是一个全局计划。这就是我在做什么。有什么问题吗?
NumberFormatInfo MyNFI = new NumberFormatInfo();
MyNFI.NegativeSign = "-";
MyNFI.NumberDecimalSeparator = ".";
MyNFI.NumberGroupSeparator = ",";
MyNFI.CurrencySymbol = "$";
decimal d = decimal.Parse("$45.00", MyNFI); // throws exception here...
最佳答案
如何使用:
decimal d = decimal.Parse("$45.00", NumberStyles.Currency);
有关 Decimal.Parse 的 MSDN 文档状态:
"The s parameter is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method
关于c# - 将货币文本解析为十进制类型时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4953037/