Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

Math.Floor() and Math.Ceiling() methods

Reference for Math.Floor, Math.Ceiling and Math.Truncate in C#: round toward negative or positive infinity, negative number behavior and integer division.

Math.Floor rounds a number down to the nearest integer value that is not larger than it. Math.Ceiling rounds up to the nearest integer value that is not smaller. Both return a double (or decimal) with no fractional part, and both leave numbers that already are whole unchanged.

For negative numbers, "down" means toward negative infinity: Math.Floor(-2.1) is -3, and Math.Ceiling(-2.9) is -2. If you want to simply drop the fraction, use Math.Truncate, which rounds toward zero.

C#
Math.Floor(2.7);      // 2
Math.Ceiling(2.1);    // 3
Math.Floor(-2.1);     // -3
Math.Truncate(-2.7);  // -2

Try it

C# Floor, Ceiling and Truncate compared

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Pages, pagination and integer division

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static double  Floor(double d);
public static decimal Floor(decimal d);
public static double  Ceiling(double a);
public static decimal Ceiling(decimal d);
public static double  Truncate(double d);
public static decimal Truncate(decimal d);
Name Description
d / a The number to round.

Returns: Floor returns the largest integral value less than or equal to the argument. Ceiling returns the smallest integral value greater than or equal to it. The return type is the same as the argument type, never an integer type.

Errors / undefined behavior: none for double: NaN, +Infinity and -Infinity are returned unchanged. Nothing is thrown, but casting a result that is out of range to int gives an unreliable value (or an OverflowException in a checked context), so check the range first.

Usage notes

  • Directions. Floor goes toward negative infinity, Ceiling toward positive infinity, Truncate toward zero. For positive numbers Floor and Truncate agree; for negative numbers Ceiling and Truncate agree.
  • Pagination. Math.Ceiling(total / (double)pageSize) is the classic page count. Note the cast: total / pageSize with two ints truncates before Ceiling can help, which is why the second example casts first. The integer form (total + size - 1) / size avoids floating point.
  • Integer division is not Floor. In C#, -7 / 2 is -3 (truncation). If you need floor division for negatives, use Math.Floor on a double, or adjust the integer result when there is a remainder with the opposite sign.
  • Snap to a grid. Math.Floor(x / step) * step rounds down to a multiple. It may add small floating-point noise for steps like 0.1; use decimal there.
  • Precision limits. A double above 2^52 has no fractional part to round; Floor returns it as is. For long values above 2^53, converting to double first loses digits.
  • Negative zero. Math.Ceiling(-0.5) returns -0, which prints as -0 and equals 0.
  • Related names. MathF.Floor is for float. Generic math exposes double.Floor and TNumber.Floor as static members.
  • Casting. (int)x truncates and is the fastest way to drop a fraction when you already know the range. Use Floor when negative inputs must round down.