Just use the overload of decimal.TryParse
which takes an IFormatProvider
, and specify the invariant culture. Then use that for formatting, too. (I'm assuming you want it to be treated consistently as "dot for the decimal separator" on both parsing and formatting.)
Oh, and only parse once - why do it twice?
decimal number = 0;
if (Decimal.TryParse(textBox20.Text, NumberStyles.Number,
CultureInfo.InvariantCulture, out number))
{
textBox20.Text = number.ToString("0.00", CultureInfo.InvariantCulture);
}
else
{
textBox20.Text = "0.00";
}
Given that 0 will be formatted that way as well, you don't even need anything to be conditional:
decimal number;
decimal.TryParse(textBox20.Text, NumberStyles.Number,
CultureInfo.InvariantCulture, out number);
textBox20.Text = number.ToString("0.00", CultureInfo.InvariantCulture);
We don't care about the return value of TryParse
in this case, because if it fails, number
will be 0, which is fine...