Skip to main content

Culture-Specific Formatting

  • 3 minutes to read

In .NET applications, formatting options for currency, numbers, and dates are not related to the application’s UI language specified as the Thread.CurrentUICulture property value. The formatting options that are set in the current user’s operating system or passed by the Internet browser are used. You can also specify a specific formatting culture or customize the default settings.

Change the Formatting Culture

You can set another formatting culture using the XafApplication.SetFormattingCulture method, which changes the Thread.CurrentCulture value. The following code demonstrates how to do this in a Windows Forms application project’s Program.cs (Program.vb) file:

static void Main() {
    // ...
    MySolutionWindowsFormsApplication winApplication = 
        new MySolutionWindowsFormsApplication();
    winApplication.SetFormattingCulture("de");
    // ...
}

The following image illustrates the result.

CustomizeCultureWin2

Similarly, you can change the formatting culture in an ASP.NET Web Forms application project’s Global.asax.cs (Global.asax.vb) file:

protected void Session_Start(Object sender, EventArgs e) {
    WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
    WebApplication.Instance.SetFormattingCulture("de");
    // ...
}

Note

In .NET 5, the libraries used for globalization functionality have been changed. Refer to the following topic for details: Globalization APIs use ICU libraries on Windows.

If a current thread’s culture is set to a culture that includes only the language and not the country (for example, “de” or “en”), the currency symbol renders as an international currency symbol (¤), for example: 100.00 ¤.

Refer to the following topics for details:

Tip

For ASP.NET Core Blazor UI applications

Refer to the following articles for information on how to change the language at runtime:

Override the Default Formatting Options

You can override the default formatting options in the XafApplication.CustomizeFormattingCulture event handler. The following code demonstrates how to do this in a Windows Forms application project’s Program.cs (Program.vb) file:

public static void Main() {
    //...
    MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication(); 
    winApplication.CustomizeFormattingCulture += 
        new EventHandler<CustomizeFormattingCultureEventArgs>(
            winApplication_CustomizeFormattingCulture);
      // ...
}
static void winApplication_CustomizeFormattingCulture(
    object sender, CustomizeFormattingCultureEventArgs e) {
    e.FormattingCulture.NumberFormat.CurrencySymbol = "USD";
}

The following image illustrates the currency symbol before and after implementing the code above:

CustomizeCultureWin

The following code demonstrates how to override the default formatting options in an ASP.NET Web Forms application project’s Global.asax.cs (Global.asax.vb) file:

protected void Session_Start(Object sender, EventArgs e) {
    WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
    WebApplication.Instance.CustomizeFormattingCulture += Instance_CustomizeFormattingCulture;
    // ...
    WebApplication.Instance.Setup();
    WebApplication.Instance.Start();
}

The following image illustrates the currency symbol after implementing the code above:

CustomizeCultureWeb