Search code examples
c#.netlocale

What controls the CurrencyPositivePattern in .NET


When running

var x = 10.0M;

Console.WriteLine(typeof(Program).Assembly.ImageRuntimeVersion);

var culture = System.Globalization.CultureInfo.GetCultureInfo("da-DK");

Console.WriteLine(culture.NumberFormat.CurrencyPositivePattern);
Console.WriteLine(x.ToString("C", culture));

We see a difference when using different framework versions.

v2.0.5072
2
kr 10,00

vs

v4.0.30319
3
10,00 kr.

Also why would I see different a NumberFormat.CurrencyPositivePattern between machines. Is it framework specific or related to the OS?

40,00 kr. vs kr. 20,00


Solution

  • Also why would I see different a NumberFormat.CurrencyPositivePattern between machines. Is it framework specific or related to the OS?

    You are using an override (GetCultureInfo) that should NOT be affected by user preferences. According to MSDN, culture specific values are inherently unstable. I think that what you see is more of an OS thing, could change between Windows Updates. If you need something stable, in unit test for example, invariant or custom culture is the best bet.

    NumberFormatInfo and dynamic data

    The culture-specific data for formatting numeric values provided by the NumberFormatInfo class is dynamic, just like the cultural data provided by the CultureInfo class. You should not make any assumptions about the stability of values for NumberFormatInfo objects that are associated with particular CultureInfo objects. Only the data provided by the invariant culture and its associated NumberFormatInfo object is stable. Other data can change between application sessions, or even within a single session, for the following reasons:

    • System updates. Cultural preferences such as the currency symbol or currency formats change over time. When this happens, Windows Update includes changes to the NumberFormatInfo property value for a particular culture.

    • Replacement cultures. The CultureAndRegionInfoBuilder class can be used to replace the data of an existing culture.

    • Cascading changes to property values. A number of culture-related properties can change at run time, which, in turn, causes NumberFormatInfo data to change. For example, the current culture can be changed either programmatically or through user action. When this happens, the NumberFormatInfo object returned by the CurrentInfo property changes to an object associated with the current culture.

    • User preferences. Users of your application might override some of the values associated with the current system culture through the region and language options in Control Panel. For example, users might choose a different currency symbol or a different decimal separator symbol. If the CultureInfo.UseUserOverride property is set to true (its default value), the properties of the NumberFormatInfo object are also retrieved from the user settings.