1

This question could be an exact copy pase from this question: Get number format from OS But it seems I can't find a suitable solution for C#. I dont want to end up using the registry of the OS to make it work. And since I am in C# the Java's solution is not possible.

Take note that

System.Globalization.CultureInfo.CurrentCulture;
System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.CultureInfo.CurrentUICulture;

none of them returns the modified OS preference.

Exemple: en-CA the decimal is '.' and grouping would be ','. If i switch them arround it is not possible to recover the modification via InstalledUICulture, CurrentUICulture and CurrentCulture

Edit: here is the informations I am looking to get from the OS Yes it is a copy of the image from the other question enter image description here

Community
  • 1
  • 1
Daneau
  • 1,085
  • 9
  • 15

2 Answers2

1

As stated in this MSDN article, the CultureInfo.InstalledUICulture property gets the CultureInfo that represents the culture installed with the operating system.

This property has a NumberFormat field that contains the information you are looking for.

Wassim H
  • 139
  • 8
  • Remove the first part, it should be a comment. The rest looks like a decent answer. – BradleyDotNET Aug 20 '15 at 16:44
  • If you do the modification on your Windows OS like the picture on your PC and then start your IDE (even if you restart your PC) the NumberFormat Group and decimal separator won't represent what is under your preference that you just modified – Daneau Aug 20 '15 at 17:04
0
void Main()
{
    DescribeIt(System.Globalization.CultureInfo.InstalledUICulture);
    DescribeIt(System.Globalization.CultureInfo.CurrentUICulture);
    DescribeIt(new System.Globalization.CultureInfo("en-US"));
    DescribeIt(new System.Globalization.CultureInfo("fr-FR"));
}

public void DescribeIt(System.Globalization.CultureInfo ci)
{
    Console.Write("Culture: {0}", ci);
    Console.Write(@"; NumberFormat.NumberGroupSeparator: ""{0}""", 
        ci.NumberFormat.NumberGroupSeparator);
    Console.Write(@"; NumberFormat.NumberDecimalSeparator: ""{0}""", 
        ci.NumberFormat.NumberDecimalSeparator);
    Console.WriteLine();        
}

Results on my machine:

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: fr-FR
  NumberFormat.NumberGroupSeparator: " "
  NumberFormat.NumberDecimalSeparator: ","

If I change the Decimal symbol to "." and Digit grouping to "," in the fr-FR settings in the Formats tab of the Region and Language Control Panel dialog, and then run the same code, I get this:

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: en-US
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

Culture: fr-FR
  NumberFormat.NumberGroupSeparator: ","
  NumberFormat.NumberDecimalSeparator: "."

What results are you getting?

Edmund Schweppe
  • 4,992
  • 1
  • 20
  • 26
  • Your Answer works. But i do not understand why the InstalledUICulture does not represent the one that i changed in my option (the default one). Forcing it to en-CA works perfectly fine. Do you have any idea of the reason ? (I need to avoid forcing a specific culture) – Daneau Aug 20 '15 at 17:29
  • What **is** your `InstalledUICulture`? – Edmund Schweppe Aug 20 '15 at 17:37
  • InstalledUICulture returns en-US while the culture i am using on my os by default is "Match Windows display language" (this is the "recommended" one and thus the one by default). Facts are that "default" one doesn't modify the one that is represented by InstalledUICulture and this is problematic – Daneau Aug 20 '15 at 17:41