Search code examples
c#cultureinfo

Different formatting on different computers when using the same CultureInfo, why?


I'm having an issue when using CultureInfo.

This is basically my code:

Console.WriteLine(0.5.ToString("P2", new CultureInfo("en-US")));

When running it, this is what I get on two different computers:

As you can notice, the formatting isn't the same. The Australia VPS ( running Windows Server 2016) having a space before the percent symbol compared to my computer (Windows 10).

Why that? How can I "really" use the same CultureInfo/formatting everywhere?


Solution

  • The CultureInfo on Windows uses machine/user level locale information and does not have any information on its own. This information is updated relatively frequently by OS versions and updates. As result machines with different levels of patches or different versions of OS will likely have differences, usually minor as you see but sometime critical if something like decimal separator changes between '.' and ',' or currency symbol for a region changes.

    Usually it is not a problem as such formatting is used only to show/parse values from the user and not to store data anywhere (which uses InvariantCulture). Most individual users would not ever use enough machines to notice difference in desktop apps. And for server side code (i.e. ASP.Net) running multiple servers with different versions of OS is even less likely.

    If you really must show identical formats independent of OS you would have to build your own CultureInfo objects for locales you interested in. In many cases you can just create your own CultureInfo based on existing one and just patch properties you care about.