简体   繁体   中英

FormatException after parsing decimal

I have a problem with this operation on one computer:

string S1 = "000,00"; decimal 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)

It works on other computers.

I tried everything that came to my mind, among others:

Decimal.Parse(S1, CultureInfo.InvariantCulture);

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

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

Changing ',' to '.' Tried decimal.Parse, Double.Parse, double.Parse = the same problem.

I've had the similar problem some time ago. The options CultureInfo.InvariantCulture and NumberStyles.Any solved the problem.

Providing the InvariantCulture causes parsing to use the ',' character as the thousands separator. Providing NumberStyles.Number allows number format, which includes the AllowThousands option:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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