Arithmetic Methods
<< Back
// Determines if the specified string represents a numeric value.
// Allows for negative and decimal numbers.
public static bool IsNumber (this string str)
{
   return new Regex(@"\b\-?\d+(?:\.\d+)?\b|\b\.\d+\b").IsMatch(str);
} 

 

// Calculates the factorial of a number.
// Accepts a number between 0 and 20.
public static long Factorial (int val)
{
    if( val.IsBetween(0, 20).False() ) { throw new ArgumentOutOfRangeException("val"); }
    long factorial = val;
    for( int i = val - 1; i > 1; i-- )
    {
    	factorial *= i;
    }
    return factorial;
}