public string Calculate(int digits)
{
StringBuilder pi = new StringBuilder("3", digits +
2);
string result = null;
try
{
if (digits > 0)
{
// TODO: Add Tracing around the calculation
using (new Tracer(Category.Trace,
"Calculate PI"))
{
pi.Append(".");
for (int i = 0; i < digits; i += 9)
{
CalculatingEventArgs args;
args = new
CalculatingEventArgs(pi.ToString(), i + 1);
OnCalculating(args);
// Break out if cancelled
if (args.Cancel == true) break;
// Calculate next 9 digits
int nineDigits =
NineDigitsOfPi.StartingAt(i+1);
int digitCount = Math.Min(digits -
i, 9);
string ds = string.Format("{0:D9}",
nineDigits);
pi.Append(ds.Substring(0,
digitCount));
}
}
}
result = pi.ToString();
// Tell the world I've finished!
OnCalculated(new CalculatedEventArgs(result));
}
catch (Exception ex)
{
// Tell the world I've crashed!
OnCalculatorException(new
CalculatorExceptionEventArgs(ex));
}
return result;
}