从OS C#获取数字格式

时间:2015-08-20 16:19:11

标签: c# operating-system number-formatting

这个问题可能是这个问题的精确复制品: Get number format from OS 但似乎我无法为C#找到合适的解决方案。我不想最终使用操作系统的注册表来使其工作。因为我在C#中,Java的解决方案是不可能的。

请注意

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

它们都没有返回修改后的OS首选项。

例子:en-CA小数是'。'分组将是',#39;。如果我在周围切换它们,则无法通过InstalledUICulture,CurrentUICulture和CurrentCulture恢复修改

编辑:这是我希望从操作系统中获取的信息是的,它是来自其他问题的图像的副本 enter image description here

2 个答案:

答案 0 :(得分:0)

如此MSDN article中所述,CultureInfo.InstalledUICulture属性获取CultureInfo,该文件表示随操作系统一起安装的文化。

此属性具有NumberFormat字段,其中包含您要查找的信息。

答案 1 :(得分: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();        
}

我的机器上的结果:

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: ","

如果我将Decimal symbol更改为“。”和Digit grouping到“,”在“区域和语言控制面板”对话框的“格式”选项卡中的fr-FR设置中,然后运行相同的代码,我得到:

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: "."

你得到了什么结果?