Decimal group seperator for the fractional part

2019-03-05 10:35发布

问题:

I wonder what would be the best way to format numbers so that the NumberGroupSeparator would work not only on the integer part to the left of the comma, but also on the fractional part, on the right of the comma.

Math.PI.ToString("###,###,##0.0##,###,###,###")  // As documented ..
                                                // ..this doesn't work
3.14159265358979                                // result
3.141,592,653,589,79                            // desired result

As documented on MSDN the NumberGroupSeparator works only to the left of the comma. I wonder why??

回答1:

A little clunky, and it won't work for scientific numbers but here is a try:

class Program
{
    static void Main(string[] args)
    {
        var π=Math.PI*10000;

        Debug.WriteLine(Display(π));
        // 31,415.926,535,897,931,899
    }

    static string Display(double x)
    {
        int s=Math.Sign(x);
        x=Math.Abs(x);
        StringBuilder text=new StringBuilder();
        var y=Math.Truncate(x);
        text.Append((s*y).ToString("#,#"));
        x-=y;
        if (x>0)
        {
            // 15 decimal places is max reasonable precision
            y=Math.Truncate(x*Math.Pow(10, 15));
            text.Append(".");
            text.Append(y.ToString("#,#").TrimEnd('0'));
        }
        return text.ToString();
    }

}


回答2:

It might be best to work with the string generated by your .ToString():

class Program
{
    static string InsertSeparators(string s)
    {
        string decSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;

        int separatorPos = s.IndexOf(decSeparator);
        if (separatorPos >= 0)
        {
            string decPart = s.Substring(separatorPos + decSeparator.Length);
            // split the string into parts of 3 or less characters
            List<String> parts = new List<String>();
            for (int i = 0; i < decPart.Length; i += 3)
            {
                string part = "";
                for (int j = 0; (j < 3) && (i + j < decPart.Length); j++)
                {
                    part += decPart[i + j];
                }
                parts.Add(part);
            }
            string groupSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator;
            s = s.Substring(0, separatorPos) + decSeparator + String.Join(groupSeparator, parts);
        }

        return s;

    }

    static void Main(string[] args)
    {

        for (int n = 0; n < 15; n++)
        {
            string s = Math.PI.ToString("0." + new string('#', n));
            Console.WriteLine(InsertSeparators(s));
        }

        Console.ReadLine();

    }
}

Outputs:

3
3.1
3.14
3.142
3.141,6
3.141,59
3.141,593
3.141,592,7
3.141,592,65
3.141,592,654
3.141,592,653,6
3.141,592,653,59
3.141,592,653,59
3.141,592,653,589,8
3.141,592,653,589,79


回答3:

OK, not my strong side, but I guess this may be my best bet:

 string input = Math.PI.ToString();
 string decSeparator = System.Threading.Thread.CurrentThread
                      .CurrentCulture.NumberFormat.NumberGroupSeparator;
 Regex RX = new Regex(@"([0-9]{3})");
 string result  = RX.Replace(input , @"$1" + decSeparator);

Thanks for listening..