C#格式化四位小数

Format four decimal places in C#

我有这个算法,我需要把它格式化成小数点后4位小数,我有这个算法怎么办?

假设一个输入是:2.00,那么我的输出一定是12.5664

static void Main(string[] args)
        {
            double n = 3.14159;
            double raio = double.Parse(Console.ReadLine());

            double area = n * Math.Pow(Convert.ToDouble(raio), 2);

            Console.WriteLine($"A={area}");
        }

您可以将其作为格式字符串传递给您想要的小数位数:

        double n = 3.14159;
        double raio = double.Parse(Console.ReadLine());
        
        //fixed converting a double from a variable that was already a double
        double area = n * Math.Pow(raio, 2);

        Console.WriteLine($"A={area.ToString("#.####")}");

        //if you need to get the rounded number to do another calculation with
        double roundedArea = Double.Parse(area.ToString("#.####"));

双倍面积=123.4567;

// 最大值两位小数

String.Format("{0:0.####}", 面积); // "123.4567"

String.Format("{0:0.##}", 面积); // "123.46"

String.Format("{0:0}", 面积); // "123"