Culture and Formatting in WPF DoubleTextBox

18 Nov 20185 minutes to read

Value of DoubleTextBox can be formatted in following ways:

  • Culture
  • NumberFormatInfo
  • Dedicated properties (NumberGroupSeparator, NumberGroupSizes, NumberDecimalDigits, NumberDecimalSeparator)

Culture based formatting

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

<syncfusion:DoubleTextBox x:Name="doubleTextBox" Height="25" Width="150" Culture="bs-Latn" Value="1234567"/>
using Syncfusion.Windows.Shared;
using System.Globalization;

DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 150;
doubleTextBox.Height = 25;
doubleTextBox.Value = 1234567;

//Setting Latin culture for double textbox.
doubleTextBox.Culture = new CultureInfo("bs-Latn");

By default the US culture uses “,” as the NumberGroupSeparator and “.” as the NumberDecimalSeparator, whereas the Latin culture uses “.” as the NumberGroupSeparator and “,” as the NumberDecimalSeparator.

Default Culture

WPF DoubleTextBox with Default Culture

Latin Culture

WPF DoubleTextBox with Latin Culture

NumberFormatInfo based formatting

The number formatting of DoubleTextBox 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="DoubleTextBoxSample.MainWindow"
        Title="DoubleTextBox Sample" Height="350" Width="525">
    <Grid>
        <syncfusion:DoubleTextBox x:Name="doubleTextBox" Height="25"
                                  Width="200" Value="1234567"
                                  GroupSeperatorEnabled = "True">
            <syncfusion:DoubleTextBox.NumberFormat>
                <numberformat:NumberFormatInfo NumberGroupSeparator="/"
                                               NumberDecimalDigits="4"
                                               NumberDecimalSeparator="*"/>
            </syncfusion:DoubleTextBox.NumberFormat>
        </syncfusion:DoubleTextBox>
    </Grid>
</Window>
using Syncfusion.Windows.Shared;
using System.Globalization;

DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 200;
doubleTextBox.Height = 25;
doubleTextBox.Value = 1234567;
doubleTextBox.GroupSeperatorEnabled = true;
doubleTextBox.NumberFormat = new NumberFormatInfo()
{
    NumberGroupSeparator = "/",
    NumberDecimalDigits = 4,
    NumberDecimalSeparator = "*"
};

WPF DoubleTextBox with Formatting

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

using Syncfusion.Windows.Shared;
using System.Globalization;

DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 200;
doubleTextBox.Height = 25;
doubleTextBox.Value = 123456789;
doubleTextBox.NumberFormat = new NumberFormatInfo()
{
    NumberDecimalDigits = 4,
    NumberGroupSeparator = "/",
    NumberDecimalSeparator = "*",

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

WPF DoubleTextBox displays Different Group Size between Numbers

Formatting with dedicated properties

The number formatting of DoubleTextBox can also be customized by setting the NumberGroupSeparator, NumberGroupSizes, NumberDecimalDigits, and NumberDecimalSeparator properties. You can show the group separator by enabling the GroupSeperatorEnabled property to true. The default value of GroupSeperatorEnabled is false.

The following code illustrates how to format using the NumberDecimalSeparator, NumberDecimalDigits, NumberGroupSeparator, and NumberGroupSizes properties of the DoubleTextBox.

using Syncfusion.Windows.Shared;
using System.Globalization;

DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 150;
doubleTextBox.Height = 25;
doubleTextBox.Value = 123456789;
doubleTextBox.NumberGroupSeparator = "/";
doubleTextBox.NumberDecimalSeparator = "*";
doubleTextBox.NumberDecimalDigits = 3;

// Adding the Number group size via NumberGroupSizes property.
doubleTextBox.NumberGroupSizes = new int[] { 4, 3, 2 };

WPF DoubleTextBox with Formatting

NOTE

When you use both the NumberFormat and the dedicated properties (NumberGroupSeparator, NumberGroupSizes, NumberDecimalDigits, and NumberDecimalSeparator) to format the value of DoubleTextBox, the dedicated properties take priority over NumberFormat.

NOTE

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