【问题标题】:NumberGroupSizes for "en-IN" culture in windows server 2012 is wrongWindows Server 2012 中“en-IN”区域性的 NumberGroupSizes 错误
【发布时间】:2017-02-13 11:18:07
【问题描述】:

“en-IN”区域性的 NumberGroupSizes 设置为 3,2,0,这是错误的,最好在 Windows Server 2012 中设置为 3,2。

// Gets a NumberFormatInfo associated with the en-IN culture.
NumberFormatInfo nfi = new CultureInfo("en-IN", false).NumberFormat;

// Displays a value with the default separator (".").
Int64 myInt = 123456789012345;

Console.WriteLine(myInt.ToString("N", nfi));

上面的代码在 windows server 2012 上运行,输出为 1234567890,12,345.00,这是错误的。理想情况下应该是 12,34,56,78,90,12,345.00

【问题讨论】:

  • 听起来很烦人,但编程问题是什么?
  • 我刚刚编辑了带有代码详细信息的问题@MSalters
  • 看起来更像是一个可以回答的问题。不幸的是不是我,但 StackOverflow 有成千上万的 C# 专家。
  • 这是一个操作系统设置,使用控制面板>语言配置。
  • 看起来像是 2012 服务器的问题,因为它在其他服务器上运行良好。

标签: c# windows-server-2012 cultureinfo


【解决方案1】:

这背后的原因是存储在NumberFormatInfo.NumberGroupSizes 属性中的值。对于文化“en-IN”,此属性的值为 {3,2,0},这意味着小数点后的第一组数字为 3 位,下一组为 2 位,其余数字不会被分组。

您可以通过运行此代码进行检查。

public static void Main()
{
    NumberFormatInfo nfi = new CultureInfo("en-IN", false).NumberFormat;

    Int64 myInt = 123456789012345;

    Console.WriteLine("NumberGroupSizes.Length : {0}", nfi.NumberGroupSizes.Length);
    for(var i = 0;i<nfi.NumberGroupSizes.Length; i++)
    {
        Console.WriteLine("NumberGroupSizes[{0}] : {1}", i, nfi.NumberGroupSizes[i]);
    }
    Console.WriteLine(myInt.ToString("N",nfi));

如果您使用“en-US”区域性创建 NumberFormatInfo,它将在“NumberGroupSizes”属性中只有一个值,并且该值为“3”,因此输出会将数字分成 3 位数的组。

NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

Int64 myInt = 123456789012345;

Console.WriteLine(myInt.ToString("N", nfi));
// The output will 123,456,789,012,345.00

要解决您的问题,您需要为 NumberFormatInfo 的 NumberGroupSizes 属性设置新值,如下所示。

public static void Main()
{
    NumberFormatInfo nfi = new CultureInfo("en-IN", false).NumberFormat;

    Int64 myInt = 123456789012345;

    int[] x = {3,2};
    nfi.NumberGroupSizes = x;
    Console.WriteLine(myInt.ToString("N",nfi));
    //The output will be 12,34,56,78,90,12,345.00
}

希望这能解决您的问题。

【讨论】:

  • 谢谢@Chetan,但这就是问题所在。为什么将 en-IN 设置为 3,2,0?在所有其他服务器中,其设置为 3,2
  • 这可能是因为服务器的当前数字格式设置和文化。您是否看到所有其他服务器和此服务器之间的区别?让我回到你身边。
  • 是的,它在 2012 年的所有服务器中都不同。 2008服务器工作正常,甚至本地系统设置与2008服务器匹配。
  • 真的很奇怪。所有 Windows 2012 服务器都有这个问题还是只有特定的服务器?您能否检查“控制面板 > 时钟、语言和区域 -> 其他设置”下的“数字分组”设置。这个问题现在值得赏金:)
  • 我也在 2008 R2 和 2012 Azure VM 上进行了尝试。它在 2008 R2 上运行良好,但在 2012 上运行良好。看起来这是操作系统的问题,而不是代码的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多