How to calculate pi to N number of places in C# using loops

Viewed 18012

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. Thanks in advance for the help.

6 Answers
Related