How might I go about calculating PI in C# to a certain number of decimal places?
I want to be able to pass a number into a method and get back PI calculated to that number of decimal places.
public decimal CalculatePi(int places)
{
// magic
return pi;
}
Console.WriteLine(CalculatePi(5)); // Would print 3.14159
Console.WriteLine(CalculatePi(10)); // Would print 3.1415926535
etc…
I don’t care about the speed of the program. I just want it to be as simple and easy to understand as it can be.
After much searching I found this little snippet:
It is working like a charm so far. You just have to add the System.Numerics library from the GAC to resolve the BigInteger type.