With reg to this question Pi in C#
I coded the below code and gives a output with last 6 digits as 0. So I want to improve the program by converting everything to decimal. I have never used decimal in C# instead of a double before and I am only comfortable with double in my regular use.
So please help me with decimal conversion, i tried to replace all double to decimal at start and it didnt turn good :(.
using System;
class Program
{
static void Main()
{
Console.WriteLine(" Get PI from methods shown here");
double d = PI();
Console.WriteLine("{0:N20}",
d);
Console.WriteLine(" Get PI from the .NET Math class constant");
double d2 = Math.PI;
Console.WriteLine("{0:N20}",
d2);
}
static double PI()
{
// Returns PI
return 2 * F(1);
}
static double F(int i)
{
// Receives the call number
//To avoid so error
if (i > 60)
{
// Stop after 60 calls
return i;
}
else
{
// Return the running total with the new fraction added
return 1 + (i / (1 + (2.0 * i))) * F(i + 1);
}
}
}
Output
Get PI from methods shown here 3.14159265358979000000 Get PI from the .NET Math class constant 3.14159265358979000000