C# Math
The static System.Math class bundles the mathematical functions you need most often: powers, roots, rounding, absolute values, trigonometry and more. You call its methods through the class name, for example Math.Sqrt(16).
Common Math methods
| Method | Description | Example | Result |
|---|---|---|---|
Math.Max(a, b) |
Larger of two values | Math.Max(3, 8) |
8 |
Math.Min(a, b) |
Smaller of two values | Math.Min(3, 8) |
3 |
Math.Abs(x) |
Absolute value | Math.Abs(-5) |
5 |
Math.Sqrt(x) |
Square root | Math.Sqrt(81) |
9 |
Math.Pow(x, y) |
x to the power y | Math.Pow(2, 10) |
1024 |
Math.Round(x) |
Round to nearest | Math.Round(2.7) |
3 |
Math.Floor(x) |
Round down | Math.Floor(2.7) |
2 |
Math.Ceiling(x) |
Round up | Math.Ceiling(2.1) |
3 |
Math.Clamp(x, lo, hi) |
Limit to a range | Math.Clamp(15, 0, 10) |
10 |
using System;
Console.WriteLine(Math.Max(12, 7));
Console.WriteLine(Math.Min(-1, 4));
Console.WriteLine(Math.Abs(-8.5));
Console.WriteLine(Math.Sqrt(144));
Console.WriteLine(Math.Pow(3, 4));
Console.WriteLine(Math.Clamp(150, 0, 100));12
-1
8.5
12
81
100
Note that Math.Sqrt and Math.Pow always return a double, even for whole-number inputs. Cast the result if you need an int: (int)Math.Pow(2, 8).
Rounding
There are several ways to turn a fractional number into a whole one, and they differ for negative numbers and halfway values.
using System;
double[] values = { 2.4, 2.5, 3.5, -2.5, -2.7 };
foreach (double v in values)
{
Console.WriteLine(quot;{v,5}: Round={Math.Round(v),3} Floor={Math.Floor(v),3} Ceiling={Math.Ceiling(v),3} Truncate={Math.Truncate(v),3}");
} 2.4: Round= 2 Floor= 2 Ceiling= 3 Truncate= 2
2.5: Round= 2 Floor= 2 Ceiling= 3 Truncate= 2
3.5: Round= 4 Floor= 3 Ceiling= 4 Truncate= 3
-2.5: Round= -2 Floor= -3 Ceiling= -2 Truncate= -2
-2.7: Round= -3 Floor= -3 Ceiling= -2 Truncate= -2
By default Math.Round uses banker's rounding: exact halves go to the nearest even number, so 2.5 becomes 2 and 3.5 becomes 4. This avoids a statistical bias when rounding many values. If you want the "school" rule, say so explicitly:
using System;
Console.WriteLine(Math.Round(2.5));
Console.WriteLine(Math.Round(2.5, MidpointRounding.AwayFromZero));
Console.WriteLine(Math.Round(3.14159, 2));
Console.WriteLine(Math.Round(1.005m, 2, MidpointRounding.AwayFromZero));2
3
3.14
1.01
The second argument of Math.Round is the number of decimal places to keep.
Constants and trigonometry
Math.PI and Math.E hold the famous constants. Trigonometric functions work in radians, so convert degrees first.
using System;
double radius = 3;
double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine(quot;Area: {area:F2}");
double degrees = 30;
double radians = degrees * Math.PI / 180;
Console.WriteLine(quot;sin(30°) = {Math.Sin(radians):F3}");
Console.WriteLine(quot;e = {Math.E:F5}");Area: 28.27
sin(30°) = 0.500
e = 2.71828
The :F2 after an expression in an interpolated string formats the number with two decimals — more on that in the String Interpolation lesson.
Integer helpers
Some handy methods for whole numbers live on the types themselves or in Math:
| Call | Result | Notes |
|---|---|---|
Math.DivRem(17, 5) |
(3, 2) |
Quotient and remainder as a tuple |
int.IsEvenInteger(6) |
True |
.NET 7+ helper |
Math.Sign(-9) |
-1 |
-1, 0 or 1 |
Math.Log10(1000) |
3 |
Base-10 logarithm |
Tip: For random numbers use
Random.Shared.Next(1, 7)(a die roll from 1 to 6). Our playgrounds avoid randomness so that outputs are repeatable.
Test yourself
What does Math.Round(4.5) return by default?
- 5
- 4
- 4.5
- An error
What is the return type of Math.Pow(2, 3)?
- int
- long
- double
- decimal
Which method always rounds up to the next whole number?
- Math.Floor
- Math.Ceiling
- Math.Truncate
- Math.Round