-1

In a WPF/xaml code, I can display a certain amount of money with:

<TextBlock Text="{Binding ., StringFormat={}{0:C}}" xml:lang="fr-FR"/>

It will display for instance:

680 000,00 €

I'm trying to get rid of unwanted decimals from the xaml code eg.,

680 000 €

or

680 k€

How can I do this ?

Soleil
  • 6,404
  • 5
  • 41
  • 61
  • 2
    `C` format specifier is using 2 by default as `CurrencyDecimalDigits`, per [msdn](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString). You can get rid of currency specifier and create your own converter for money – Pavel Anikhouski Oct 12 '19 at 16:31
  • @PeterDuniho Not a duplicate, as can witness the accepted answer. – Soleil Oct 13 '19 at 11:26
  • The accepted answer demonstrates your question _is_ in fact a duplicate. You asked two different question, and the duplicates clearly answer at least one of them. Saying otherwise doesn't actually make it so. – Peter Duniho Oct 13 '19 at 15:20

2 Answers2

3

If you want 680 000 € you could try: StringFormat={}{0:C0}}

For 680 k€, and for all formats you desire, the converter does the job.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
Frenchy
  • 16,386
  • 3
  • 16
  • 39
1

According your logic, you need something like that

public class CurrencyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is int intValue)
        {
            if (intValue < 1000)
            {
                return string.Format(culture, "{0:C0}", intValue);
            }

            return $"{intValue / 1000} k{culture.NumberFormat.CurrencySymbol}";
        }

        return value?.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Of course, you can make it more generic and parse millions, etc and different value types. The important point here is to pass the fr-FR culture into converter to get a correct currency symbol

Xaml declaration

<Window.Resources>
        <yournamespace:CurrencyConverter x:Key="currencyConverter"/>
</Window.Resources>

Xaml usage

<TextBlock Text="{Binding ., Converter={StaticResource currencyConverter}, ConverterCulture=fr-FR}"/>
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66