How to print a double with 4 houses after the point in C #

1
static void Main(string[] args)
{
    double area, raio , pi;
    pi = 3.14159;       
    raio = Convert.ToDouble(Console.ReadLine());
    area = pi * Math.Pow(raio,2);
    Console.WriteLine("A={0}\n",area.ToString("N0"));
}

The input value is 2.00 and the expected output value is 12.5664; however, what is coming out is 125,664. How do I fix this?

    
asked by anonymous 24.03.2018 / 18:18

1 answer

2

If you want 4 houses you can not use 0. I did this in a way that avoid breaking the application by typing wrong and using what the language already offers:

using static System.Console;
using static System.Math;

public class Program {
    public static void Main(string[] args) {
        if (double.TryParse(ReadLine(), out var raio)) WriteLine($"A = {PI * Pow(raio, 2):N4}");
    }
}

See in .NET Fiddle .

    
24.03.2018 / 18:26