小数部分的十进制组分隔符

时间:2014-10-05 12:14:34

标签: c# decimal comma separator fractions

我想知道格式化数字的最佳方法是什么,以便NumberGroupSeparator不仅可以在逗号左边的整数部分工作,而且还可以在逗号右边的小数部分工作

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

正如MSDN所述,NumberGroupSeparator仅适用于逗号的左侧。我想知道为什么?

3 个答案:

答案 0 :(得分:0)

有点笨重,它不会为科学数字而努力,但这是尝试:

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

}

答案 1 :(得分:0)

最好使用.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();

    }
}

输出:

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

答案 2 :(得分:0)

好吧,不是我强硬的一面,但我想这可能是我最好的选择:

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

感谢收听..