Search⌘ K
AI Features

Math Class

Explore how to use the static Math class in C# to perform a variety of mathematical operations. Learn to calculate powers, roots, and round numbers effectively. Understand how to simplify code with using static and apply functions like Floor and Ceiling for precise integer rounding.

We often need to perform mathematical calculations inside our programs. .NET provides the necessary tools out of the box.

The Math class

The System namespace contains a static class called Math with methods for performing various mathematical operations. For instance, we can calculate the square root of a number by calling the Math.Sqrt() method.

Note: The Math class is static, so all its members are also static. We use the class’s methods without instantiating it.

The Math class includes many methods. Here are some common examples:

  • Math.Pow(): This raises a number to a given power.

  • Math.Sqrt(): This finds the square root of a number.

  • Math.Cos(): This calculates the cosine of a given angle.

  • Math.Abs(): This returns the absolute value of a number.

  • Math.Cbrt(): ...