NumberGroupSizes for “en-IN” culture in windows se

2019-07-31 02:56发布

NumberGroupSizes for "en-IN" culture is set as 3,2,0 which is wrong and ideally be set as 3,2 in windows server 2012.

// 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));

The above code ran on windows server 2012 gives out put as 1234567890,12,345.00 which is wrong. Ideally it should be 12,34,56,78,90,12,345.00

1条回答
迷人小祖宗
2楼-- · 2019-07-31 03:00

The reason behind this is the values stored in NumberFormatInfo.NumberGroupSizes property. For culture "en-IN" this property has values {3,2,0} which means first group of the number left to the decimal point will have 3 digits, next group will be have 2 digits and rest of the number will not be grouped.

You can check by as running this code.

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));

If you create NumberFormatInfo using "en-US" culture it will have only one value in "NumberGroupSizes" property and that value is "3" so the output will divide the number in groups of 3 digits.

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

To solve your issue you need to set new values to the NumberGroupSizes property of the NumberFormatInfo as following.

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
}

I hope this will resolve your issue.

查看更多
登录 后发表回答