Home > other >  FormatException after parsing decimal
FormatException after parsing decimal

Time:01-25

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.

CodePudding user response:

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)
  •  Tags:  
  • Related