Math.Abs() / Math.Round() / Math.Ceiling() / Math.Floor()
| Since: | C# 1.0(2002) |
|---|
How to use Math.Abs() to get the absolute value of a number, Math.Round() to round a number, Math.Ceiling() to round up, and Math.Floor() to round down.
Syntax
using System; // Returns the absolute value (converts negative numbers to positive). Math.Abs(double value) // Rounds to the nearest integer (banker's rounding by default). Math.Round(double value) Math.Round(double value, int digits) Math.Round(double value, MidpointRounding mode) Math.Ceiling(double value) Math.Floor(double value) // Truncates toward zero (removes the fractional part). Math.Truncate(double value)
Method List
| Method | Description |
|---|---|
| Math.Abs(value) | Returns the absolute value of value. Overloads are available for int, long, double, decimal, and other numeric types. |
| Math.Round(value) | Rounds value to the nearest integer. Uses banker's rounding (round-half-to-even) by default. |
| Math.Round(value, digits) | Rounds value to digits decimal places. |
| Math.Round(value, MidpointRounding.AwayFromZero) | Performs conventional rounding where 0.5 always rounds up. |
| Math.Ceiling(value) | Returns the smallest integer greater than or equal to value (always rounds up). |
| Math.Floor(value) | Returns the largest integer less than or equal to value (always rounds down). |
| Math.Truncate(value) | Returns the integer part of value with the fractional part removed. Behaves differently from Floor for negative numbers. |
Sample Code
Program.cs
using System; // Math.Abs() — Gets the absolute value. Console.WriteLine(Math.Abs(-42)); // 42 Console.WriteLine(Math.Abs(3.14)); // 3.14 Console.WriteLine(Math.Abs(-7.5)); // 7.5 // Math.Round() — Rounds a number. Console.WriteLine(Math.Round(2.5)); // 2 (banker's rounding: rounds to even) Console.WriteLine(Math.Round(3.5)); // 4 (banker's rounding: rounds to even) // Use MidpointRounding.AwayFromZero for conventional rounding. Console.WriteLine(Math.Round(2.5, MidpointRounding.AwayFromZero)); // 3 Console.WriteLine(Math.Round(3.14159, 2)); // 3.14 (2 decimal places) // Math.Ceiling() — Rounds up (always toward positive infinity). Console.WriteLine(Math.Ceiling(4.1)); // 5 Console.WriteLine(Math.Ceiling(4.9)); // 5 Console.WriteLine(Math.Ceiling(-4.1)); // -4 (rounds toward zero) // Math.Floor() — Rounds down (always toward negative infinity). Console.WriteLine(Math.Floor(4.9)); // 4 Console.WriteLine(Math.Floor(-4.1)); // -5 (rounds away from zero) // Math.Truncate() — Returns the integer part, removing the fractional part. Console.WriteLine(Math.Truncate(4.9)); // 4 Console.WriteLine(Math.Truncate(-4.9)); // -4 (rounds toward zero, unlike Floor)
This produces the following output:
dotnet run 42 3.14 7.5 2 4 3 3.14 5 5 -4 4 -5 4 -4
Sample Code: Practical Use (Page Count Calculation)
The following example shows a practical use of Ceiling for calculating the total number of pages needed to display a dataset.
PagingHelper.cs
using System;
// Calculate the total number of pages for n items at k items per page.
int itemCount = 25;
int pageSize = 7;
int pageTotal = (int)Math.Ceiling((double)itemCount / pageSize);
Console.WriteLine($"Total pages: {pageTotal}"); // Total pages: 4
// Calculate the price after 10% tax, truncating the fractional part.
double price = 980;
double taxRate = 0.10;
double total = Math.Floor(price * (1 + taxRate));
Console.WriteLine($"Total: {total}"); // Total: 1078
This produces the following output:
dotnet run Total pages: 4 Total: 1078
Common Mistakes
Common Mistake: Math.Round() Uses Banker's Rounding by Default
Math.Round() uses banker's rounding (round-half-to-even) by default. When the value is exactly 0.5, it rounds to the nearest even number — so 2.5 rounds to 2 and 3.5 rounds to 4. Using it with the expectation of conventional rounding (always round 0.5 up) leads to bugs.
using System; // NG: default Round uses banker's rounding — 0.5 rounds to even Console.WriteLine(Math.Round(0.5)); // 0 (expected 1) Console.WriteLine(Math.Round(1.5)); // 2 Console.WriteLine(Math.Round(2.5)); // 2 (expected 3)
The corrected version looks like this:
using System; // OK: use MidpointRounding.AwayFromZero for conventional rounding Console.WriteLine(Math.Round(0.5, MidpointRounding.AwayFromZero)); // 1 Console.WriteLine(Math.Round(1.5, MidpointRounding.AwayFromZero)); // 2 Console.WriteLine(Math.Round(2.5, MidpointRounding.AwayFromZero)); // 3
This produces the following output:
dotnet run 0 2 2 1 2 3
Notes
The default behavior of Math.Round() is banker's rounding (round-half-to-even). Note that rounding 2.5 gives 2, not 3. To use conventional rounding where 0.5 always rounds up, specify MidpointRounding.AwayFromZero.
Math.Ceiling() and Math.Floor() can produce counterintuitive results with negative numbers. Ceiling(-4.1) returns -4, and Floor(-4.1) returns -5. Use Math.Truncate() to truncate toward zero. For other math methods, see Math.Max() / Min() / Pow() / Sqrt().
If you find any errors or copyright issues, please contact us.