Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format value in XAML with a decimal separator?

I have a little problem formatting double values in my XAML code.

double price = 10300.455;

This number should be displayed as 10,300.45 on US systems and as 10.300,45 on German systems.

So far I managed to limit the numbers with the following.

Binding="{Binding price, StringFormat=F2}"

But the result is 10300.45 and that is not what I had in mind. I could fix this easily using a converter, but I don't want to do that if there is another way around. Just the right Formatter would be fine.

like image 473
TalkingCode Avatar asked Dec 28 '09 09:12

TalkingCode


3 Answers

Binding="{Binding price, StringFormat=N2}" 

Try N instead of F. N is number format, which based on different cultures, automatically displays number formatting. Look at the sample code below which is a console application. However, if binding uses correct culture, you will get the correct value. F2 is fixed point notation.

    double price = 10300.455;

    Console.WriteLine(price.ToString("N2", 
        CultureInfo.CreateSpecificCulture("de-DE") ));
    // displays 10.300,46

    Console.WriteLine(price.ToString("N2",
        CultureInfo.CreateSpecificCulture("en-US") ));
    // displays 10,300.46
like image 77
Akash Kava Avatar answered Nov 02 '22 04:11

Akash Kava


For anyone wondering information regarding the different build in string formats and their usages can be seen here:

http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.100).aspx

like image 24
Jesse Carter Avatar answered Nov 02 '22 03:11

Jesse Carter


Set current system culture as global WPF culture. http://www.codeproject.com/Articles/442505/WPF-default-binding-format-culture

FrameworkElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
like image 2
dizel3d Avatar answered Nov 02 '22 04:11

dizel3d