Decimal.TryParse doesn't parse my decimal value

cadi2108 picture cadi2108 · Jul 3, 2012 · Viewed 23.4k times · Source

When I tried to convert something like 0.1 (from user in textbox), My value b is always false.

bool b = Decimal.TryParse("0.1", out value);

How can it be here to work?

Answer

Guffa picture Guffa · Jul 3, 2012

Specify the culture for the parsing. Your current culture uses some different number format, probably 0,1.

This will successfully parse the string:

bool b = Decimal.TryParse("0.1", NumberStyles.Any, CultureInfo.InvariantCulture, out value);