Many programs need some form of math to complete certain calculations or format numerical data. In a C# application, you can use the Math class to help you complete basic mathematical tasks.

The Math class contains many different math functions. These include functions related to decimal rounding, trigonometry, powers, and square roots. Here’s a list of some of the common functions that you can use.

How to Use Basic Math Functions

Some of the basic mathematical tasks you could do include finding the minimum and maximum numbers in a list. You can also round or truncate decimals to format them or find the absolute value of a number.

These math functions can be useful if you are creating classes in C# that will use formatted numerical data. Another scenario where it may be useful is if you’re saving data to a CSV file in a C# application.

You can add these examples to any type of C# application, such as console apps, ASP.NET applications, and more.

Math.Max()

The Max() function allows you to compare two numbers to determine which one has the maximum value. There are different variations of the method, which allow you to input different numerical data types into the function. This includes ints, double, floats, and more.

Here's an example of how you can use the function:

        int max = Math.Max(4, 7); 
Console.WriteLine(max); // output: 7

The Max() function always takes exactly two arguments to compare. If you want to find the maximum of an array of numbers, there are many workarounds. One of them includes manually looping through the array and using the Max() function to compare each number:

        var arrayMaxNumbers = new List<int>() { 3, 6, 1, 8, 4, 1 };
int maxNumber = arrayMaxNumbers[0];

foreach (var num in arrayMaxNumbers)
{
    maxNumber = Math.Max(maxNumber, num);
}

Console.WriteLine(maxNumber); // output: 8

Math.Min()

The Min() function works the same way as the Max() function. You can pass two numbers to the function and it will return the smallest:

        int min = Math.Min(4, 7); 
Console.WriteLine(min); // output: 4

Similarly, you will need to call the Min() function multiple times to find the smallest item in a list:

        var arrayMinNumbers = new List<int>() { 3, 6, -1, 8, 4, 1 };
int minNumber = arrayMinNumbers[0];

foreach (var num in arrayMinNumbers)
{
    minNumber = Math.Min(minNumber, num);
}

Console.WriteLine(minNumber); // output: -1

Math.Abs()

The Abs() function lets you calculate the absolute value of a number. An absolute value can never be negative, as it represents the distance a number is from 0.

This is how you can use the Abs() function:

        int absoluteNum = Math.Abs(5);
Console.WriteLine(absoluteNum); // output: 5

int absoluteNumNegative = Math.Abs(-5);
Console.WriteLine(absoluteNumNegative); // output: 5

Math.Round()

The Round() function accepts a decimal argument. You can also specify a limit to the number of decimal places it should have. The function then returns the rounded number:

        double roundedDecimals = Math.Round(40.12345, 2);
Console.WriteLine(roundedDecimals); // output: 40.12

You can also input a single number into the function. By default, the function will round the decimal to the nearest whole number:

        double roundedNum = Math.Round(40.6);
Console.WriteLine(roundedNum); // output: 41

Math.Truncate()

The Truncate() function takes in either a decimal or double data type, such as 4.5. It removes any fractional part of the number and returns the resulting integer value.

        double truncatedNum = Math.Truncate(4.5);
Console.WriteLine(truncatedNum); // output: 4

How to Use Basic Trigonometry Functions

The Math class also includes several functions that can assist you in trigonometry calculations.

Math.Sin()

The Sin() function allows you to input an angle measured in radians. The function will return the sine value of the angle:

        double sinAngle = (90 * (Math.PI)) / 180;
Console.WriteLine(Math.Sin(sinAngle)); // output: 1

Math.Cos()

The Cos() function also takes in an angle measured in radians. It then returns the cosine for that angle:

        double cosAngle = (90 * (Math.PI)) / 180;
Console.WriteLine(Math.Cos(cosAngle)); // output: 6.123

Math.Tan()

Likewise, the Tan() function takes an angle measured in radians, and returns the angle's tangent:

        double tanAngle = (30 * (Math.PI)) / 180;
Console.WriteLine(Math.Tan(tanAngle)); // output: 0.577

How to Use Power and Square Root Math Functions

You can use the Math functions to calculate the power and square root of a number.

Math.Pow()

The Pow() function calculates the power of a particular number. Here’s an example of how you can use the Pow() function:

        double powNum = Math.Pow(5, 3);
Console.WriteLine(powNum); // output: 125

In this case, the function calculates the result of 5 to the power of 3 (or, in other words, 5 * 5 * 5).

Math.Sqrt()

The Sqrt() function returns the square root of a number. For example, passing 16 into the function will return the square root of 16, which is 4:

        double sqrtNum = Math.Sqrt(16);
Console.WriteLine(sqrtNum); // output: 4

Using Math Functions for Mathematical Calculations

Existing Math functions are there to make your life easier when it comes to doing certain calculations. These aren't the only functions offered in the Math class, so feel free to explore some of the others, based on your needs.

Most programming languages include convenience functions, or full libraries, for common math operations.