Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Math.Abs() / Math.Round() / Math.Ceiling() / Math.Floor()

Math.Abs() / Math.Round() / Math.Ceiling() / Math.Floor()

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)

// Rounds up and returns the smallest integer greater than or equal to the value.
Math.Ceiling(double value)

// Rounds down and returns the largest integer less than or equal to the value.
Math.Floor(double value)

// Truncates toward zero (removes the fractional part).
Math.Truncate(double value)

Method List

MethodDescription
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

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.
// Uses banker's rounding by default: 0.5 rounds to the nearest even number.
Console.WriteLine(Math.Round(2.5));  // 2 (banker's rounding)
Console.WriteLine(Math.Round(3.5));  // 4 (banker's rounding)

// 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)

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 .