简体   繁体   English

解析十进制后出现FormatException

[英]FormatException after parsing decimal

I have a problem with this operation on one computer:我在一台计算机上执行此操作时遇到问题:

string S1 = "000,00";字符串 S1 = "000,00"; decimal D1 = Decimal.Parse(S1);十进制 D1 = Decimal.Parse(S1);

System.FormatException: (*) in System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) in System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) System.FormatException: (*) in System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) in System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)

It works on other computers.它适用于其他计算机。

I tried everything that came to my mind, among others:我尝试了所有想到的东西,其中包括:

Decimal.Parse(S1, CultureInfo.InvariantCulture); Decimal.Parse(S1, CultureInfo.InvariantCulture);

Decimal.Parse(S1, new CultureInfo("pl-PL")); Decimal.Parse(S1, new CultureInfo("pl-PL"));

Decimal.Parse(S1, new CultureInfo("en-US")); Decimal.Parse(S1, new CultureInfo("en-US"));

Changing ',' to '.'将“,”更改为“。” Tried decimal.Parse, Double.Parse, double.Parse = the same problem.尝试了 decimal.Parse、Double.Parse、double.Parse = 同样的问题。

I've had the similar problem some time ago.我前段时间遇到过类似的问题。 The options CultureInfo.InvariantCulture and NumberStyles.Any solved the problem.选项CultureInfo.InvariantCultureNumberStyles.Any解决了这个问题。

Providing the InvariantCulture causes parsing to use the ',' character as the thousands separator.提供InvariantCulture会导致解析使用 ',' 字符作为千位分隔符。 Providing NumberStyles.Number allows number format, which includes the AllowThousands option:提供NumberStyles.Number允许数字格式,其中包括 AllowThousands 选项:

double.TryParse(Value, NumberStyles.Number, CultureInfo.InvariantCulture, out result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM