格式化占位符,列和显示逗号,没有小数

时间:2016-08-26 02:59:53

标签: c#

以下是我当前输出的当前源代码。根据我的任务,在写出kCAEmitterLayerCircle时的计算后,它应该转到一个新的行,就像示例程序的图片一样。我不知道如何进行这种格式化,我还需要显示逗号。当我使用nCount=4时,它会在数字末尾显示 .00 ,而我不希望这样。

代码:

{0:n}

输出:

Current output

Final Output wanted

1 个答案:

答案 0 :(得分:0)

好的,这是解决方案..你真是太近了!我将其修改为控制台项目。根据需要进行更改。

    static void Main(string[] args)
    {
        do
        {
            Console.Write("Input a number: ");
            uint input = uint.Parse(Console.ReadLine());
            CalculateK(input);
            Console.ReadLine();
            Console.Clear();
        } while (true);
    }

    static void CalculateK(uint nCount)
    {
        uint kSum = 0;
        double kCalc = 0;
        double k = 1;
        for (int j = 1; j <= nCount; j++)
        {
            kCalc = Convert.ToUInt32(Math.Ceiling((1000) * Math.Pow(Math.E, -k / 20)));
            Console.Write("{0,2}. {1,3} ", j, kCalc);
            if(j % 4 == 0)
            {
                Console.WriteLine(); // If the counter is divisible by 4 then add a new line.
            }
            kSum += Convert.ToUInt32(kCalc);
            k++;
        }
        Console.WriteLine();
        Console.WriteLine("Sum = {0}", kSum.ToString("0,0")); // Use the ToString method and add your desired format instead.
    }