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.
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
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
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With