Culture and Formatting in WPF Currency TextBox

18 Nov 20187 minutes to read

Value of WPF Currency TextBox can be formatted in the following ways:

  • Culture
  • NumberFormatInfo
  • Dedicated properties (CurrencyGroupSeparator, CurrencyGroupSizes, CurrencyDecimalDigits, CurrencyDecimalSeparator)

Culture based formatting

The WPF Currency TextBox provides support for globalization by using the Culture property. The Culture property is used to format the decimal separator and group separator of the WPF Currency TextBox value based on the respective culture.

<syncfusion:CurrencyTextBox x:Name="currencyTextBox" Height="25" Width="150" Culture="fr-FR" Value="1234567"/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 100;
currencyTextBox.Height = 25;
currencyTextBox.Value = 1234567;
currencyTextBox.Culture = new CultureInfo("fr-FR");

By default the US culture uses “,” as the CurrencyGroupSeparator “$” as CurrencySymbol and “.” as the CurrencyDecimalSeparator where as the France culture uses “Space” as the CurrencyGroupSeparator, “€” as CurrencySymbol and “,” as the CurrencyDecimalSeparator.

Default Culture

WPF CurrencyTextBox with Default Culture

France Culture

WPF CurrencyTextBox with France Culture

NumberFormatInfo-Based Formatting

The number formatting of WPF Currency TextBox can be customized by setting the NumberFormat property.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        xmlns:numberformat="clr-namespace:System.Globalization;assembly=mscorlib"
        x:Class="CurrencyTextBoxSample.MainWindow"
        Title="CurrencyTextBox Sample" Height="350" Width="525">
    <Grid>
        <syncfusion:CurrencyTextBox x:Name="currencyTextBox" Height="25" Width="150" Value="1234567">
            <syncfusion:CurrencyTextBox.NumberFormat>
                <numberformat:NumberFormatInfo CurrencyGroupSeparator="/"
                                               CurrencyDecimalDigits="4"
                                               CurrencyDecimalSeparator="*"
                                               CurrencySymbol="$"/>
            </syncfusion:CurrencyTextBox.NumberFormat>
        </syncfusion:CurrencyTextBox>
    </Grid>
</Window>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.Value = 1234567;
currencyTextBox.NumberFormat = new NumberFormatInfo()
{
    CurrencyGroupSeparator = "/", 
    CurrencyDecimalDigits = 4,     
    CurrencyDecimalSeparator = "*", 
    CurrencySymbol = "$"
};

WPF CurrencyTextBox with Formatting

The following code illustrates how to set the currency group size by using the NumberFormat property.

using System.Globalization;

CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.Value = 123456789;
currencyTextBox.NumberFormat = new NumberFormatInfo()
{
    CurrencySymbol = "&",
    CurrencyDecimalDigits = 4,
    CurrencyGroupSeparator = "/",
    CurrencyDecimalSeparator = "*",

    // Adding the currency group size via NumberFormat property.
    CurrencyGroupSizes = new int[] { 2, 3, 4 }
};

WPF CurrencyTextBox displays Different Group Size between Numbers

Formatting with dedicated properties

The number formatting of WPF Currency TextBox can also be customized by setting the CurrencyGroupSeparator, CurrencyGroupSizes, CurrencyDecimalDigits and CurrencyDecimalSeparator, CurrencySymbol, CurrencyNegativePattern, and CurrencyPositivePattern properties of WPF Currency TextBox. You can show the group separator by enabling the GroupSeparatorEnabled property (true).

The following code illustrates how to format using the CurrencyDecimalSeparator, CurrencyDecimalDigits, CurrencyGroupSeparator, and CurrencyGroupSizes properties of the WPF Currency TextBox.

CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.Value = 123456789;
currencyTextBox.CurrencySymbol = "#";
currencyTextBox.CurrencyDecimalDigits = 4;
currencyTextBox.GroupSeparatorEnabled = true;
currencyTextBox.CurrencyGroupSeparator = "/";
currencyTextBox.CurrencyDecimalSeparator = "*";

// Adding the currency group size via CurrencyGroupSizes property.
currencyTextBox.CurrencyGroupSizes = new int[] { 4, 3, 2 };

WPF CurrencyTextBox with Formatting

When you use both the NumberFormat and the dedicated properties (CurrencyGroupSeparator, CurrencySymbol, CurrencyDecimalDigits, CurrencyDecimalSeparator and CurrencyGroupSizes) to format the value of WPF Currency TextBox, the CurrencyGroupSeparator and CurrencyGroupSizes properties have higher priority.

When you use both NumberFormat and Culture, the NumberFormat will have a higher priority.

Positive Value Pattern

You can use the CurrencyPositivePattern property to customize the location of the currency symbol and the positive currency values. In the table below,”$” denotes the symbol of the currency, and “n” denotes the number.

CurrencyPositivePattern table

Value Associated Pattern
0 $n
1 n$
2 $ n
3 n $

WPF CurrencyTextBox displays Positive Value Patterns

<syncfusion:CurrencyTextBox x:Name="currencyTextBox" Height="25" Width="150" Value="1234" CurrencyPositivePattern="3"/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.Value = 1234;
currencyTextBox.CurrencyPositivePattern = 3;

WPF CurrencyTextBox displays Positive Value Pattern

Negative Value Pattern

You can use the CurrencyNegativePattern property to customize the location of the currency symbol and the negative currency values. In the table below,”$” denotes the symbol of the currency, and “n” denotes the number.

CurrencyNegativePattern table

Value Associated Pattern
0 ($n)
1 -$n
2 $-n
3 $n-
4 (n$)
5 -n$
6 n-$
7 n$-
8 -n $
9 -$ n
10 n $-
11 $ n-
12 $ -n
13 n- $
14 ($ n)
15 (n $)

WPF CurrencyTextBox displays Negative Value Patterns

<syncfusion:CurrencyTextBox x:Name="currencyTextBox" Height="25" Width="150" Value="-1234" CurrencyNegativePattern="0"/>
CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.Value = -1234;
currencyTextBox.CurrencyNegativePattern = 0;

WPF CurrencyTextBox displays Negative Value Pattern