When you don't pass an
IFormatProvider
to the
TryParse
method, it defaults to using
CultureInfo.CurrentCulture[
^]. By default, this uses the current regional settings - I suspect that "Spanish (Chile)" uses
.
as the thousands separator, and
,
as the decimal separator, so the value is parsed as
500000000000000000
.
You're then using the
InstalledUICulture[
^] to format the value. This is the culture installed with the OS, and will not reflect the user's regional settings. This is probably set to "English (US)", which uses
,
as the thousands separator, and
.
as the decimal separator.
(There's a good description of the different culture options
on StackOverflow[
^].)
To get consistent results, you need to use the same format provider to parse and format your values. There is a
CultureInfo
parameter passed in to the
Convert
method, so you should probably use that:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return string.Empty;
string text = value.ToString();
double dbl;
if (double.TryParse(text, NumberStyles.Float | NumberStyles.AllowThousands, culture, out dbl))
{
text = dbl.ToString("N2", culture);
}
return text;
}