Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to convert a numeric value (Actually from a string variable i.e., "500000.000000000000") in a IValueConverter when my machines currency format was set to Spanish(Chile) in Region settings dialog which results in strange value something like "500,000,000,000,000,000.00" i don't know what actually wrong in my code.

My IValueConverter Converter
C#
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            string text = value.ToString();
            double dbl = 0.0;
            if (double.TryParse(text, out dbl))
                    text = dbl.ToString("N2", CultureInfo.InstalledUICulture);
            return text;
        }


Any guess why am getting this strange value?
Posted

How about something like this instead:
XML
<TextBox Text="{Binding Value, StringFormat=N2}" />
<TextBox Text="{Binding Value, StringFormat={}{0:#,#.00}}" />
 
Share this answer
 
Comments
Gold$Coin 30-Apr-15 8:19am    
The problem here is i can't specifically mention the string format because the template i am using is common for both string field and numeric field in my DataGrid since the columns are dynamically created. In my converter i will be parsing the converter value, if it is a numeric or double value i will be doing the stuff what i have specified above. so again i am having the above problem.
Kenneth Haugland 30-Apr-15 8:25am    
My answer will (almost) give the same result as your valueconverter. You only check if the value is empty, and then turn out nothing.
Gold$Coin 5-May-15 1:12am    
hi the below solution actually worked for me thanks a lot.
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:
C#
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;
}
 
Share this answer
 
Comments
Kenneth Haugland 30-Apr-15 11:09am    
Hmm, seems someone seem to think that the VS always run in US culture, by default:
http://stackoverflow.com/questions/520115/stringformat-localization-problem/520334#520334
Richard Deeming 30-Apr-15 11:18am    
CultureInfo.CurrentCulture reflects the current culture settings, but the WPF Binding markup extension ignores it for some bizarre reason. There was a Connect bug logged, but Microsoft have since deleted it.

The workaround in that post doesn't entirely work as of .NET 4 - the Run element is now bindable, and isn't a FrameworkElement, so it ignores that setting.

It's quite simple to create your own culture-aware binding markup extension, which seems to be the only real solution.
Kenneth Haugland 30-Apr-15 11:27am    
Ah, well, I always seem to end up with more ValueConverters than I really like to have :laugh:
Gold$Coin 5-May-15 1:12am    
That actually worked Richard. thanks a lot and remembering the thing what i have used already for .Tostring(CultureInfo.InstalledUICulture)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900