The System.Math Class

You can use the System.Math class to perform certain mathematical calculations. You can use its method to round numbers, get the square root of a certain number, or determine the result of x raised to a certain number. The System.Math class is a static class so you don’t have to make an instance of it to use its methods. Let’s demonstrate some of the methods of System.Math class.

Rounding Numbers

You can round a floating point number to a certain precision using the Math.Ceiling and Math.Floor methods. The Math.Ceilingmethod returns a double value and accepts a double argument which is the value to be rounded. The result will be the smallest whole value that is greater than or equal to the specified argument given. The Math.Floor rounds a double number to the smallest whole number whose value is less than or equal the given argument. The two methods are demonstrated below:

double number = 34.567;
double ceil = Math.Ceiling(number);
double floor = Math.Floor(number);

Console.WriteLine("Math.Ceiling({0}) = {1}", number, ceil);
Console.WriteLine("Math.Floor({0}) = {1}", number, floor);
Math.Ceiling(34.567) = 35
Math.Floor(34.567) = 34

If you want to round a number to a number with a specific number of decimal places, you can use the Math.Round method.

//Round 3.31674 into 2 decimal places
double number = 3.31674

Console.WriteLine(Math.Round(number, 2));
3.32

Raising a Number to a Certain Power

You can raise a number to a specified power using the Math.Pow method. The Math.Pow is a simple method that accepts two double arguments, the first one is the base, and the other one is the exponent. It returns the answer as a double. The code below demonstrates how to use this method.

for (int i = 0; i < 10; i++)
{
Console.WriteLine("2^{0} = {1}", i, Math.Pow(2, i));
}
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512

Getting the Square Root of a Number

If you want to get the square root of a number, you can use the Math.Sqrt method. The method accepts one argument which is the value the we want to find the square root of. The method returns a double value.

Console.WriteLine(Math.Sqrt(25));
5

Finding the Minimum and Maximum Numbers

The System.Math class provides you with Math.Min and Math.Max to find the smallest and largest value. Both methods can accept two arguments of any numerical data type. So by default, you can only give 2 numbers that will be used by comparison.

Console.WriteLine(Math.Min(1,2));
Console.WriteLine(Math.Max(1,2));
1
2

As a workaround for those methods limiting you to 2 numbers, you can nest the call for the methods like this:

//Get the maximum and minimum of 3 numbers
int max = Math.Max(Math.Max(1, 2), 3);
int min = Math.Min(Math.Min(1, 2), 3);

Console.WriteLine("Max = {0}", max);
Console.WriteLine("Min = {0}", min);
Max = 3
Min = 1

We can even create a function that will get the maximum and minimum numbers of any number of values.

static int GetMax(int[] numbers)
{
int maximum = numbers[0];

for (int i = 1; i < numbers.Length; i++)
{
maximum = Math.Max(maximum, numbers[i]);
}

return maximum;
}

static int GetMin(int[] numbers)
{
int minimum = numbers[0];

for (int i = 1; i < numbers.Length; i++)
{
minimum = Math.Min(minimum, numbers[i]);
}

return minimum;
}

static void Main()
{
int[] numbers = { 32, 17, 45, 10, 5 };
int max = GetMax(numbers);
int min = GetMin(numbers);
Console.WriteLine(max);
Console.WriteLine(min);
}
45
5

The GetMax and GetMin functions accept an integer array. It can accept any number of integer values. Inside the functions, we assumed that the first value in the array is the maximum and minimum values. We then enter a for loop which starts at index 1. It uses the Math.Min and Math.Max methods to determine if the current element in the loop is larger than the current value of maximum or is smaller than the current value of minimum. If so, the current value replaces the value of the maximum or minimum variable. We then return the resulting values.

Math.PI

System.Math provides a constant value PI which you can use when in certain cases such as finding the area of a circle. The area of the circle is defined as πr2 where π is the pi, and r is the radius of the circle. To calculate the area, use the following code.

double area = Math.PI * Math.Pow(radius, 2);

There are more methods available inside System.Math. For a complete reference of those methods, you can proceed the following link:
http://msdn.microsoft.com/en-us/library/system.math.aspx