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.
Math.Floor(2.7); // 2 Math.Ceiling(2.1); // 3 Math.Floor(-2.1); // -3 Math.Truncate(-2.7); // -2
Try it
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.
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
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 / pageSizewith twoints truncates beforeCeilingcan help, which is why the second example casts first. The integer form(total + size - 1) / sizeavoids floating point. - Integer division is not Floor. In C#,
-7 / 2is-3(truncation). If you need floor division for negatives, useMath.Flooron adouble, or adjust the integer result when there is a remainder with the opposite sign. - Snap to a grid.
Math.Floor(x / step) * steprounds down to a multiple. It may add small floating-point noise for steps like0.1; usedecimalthere. - Precision limits. A
doubleabove 2^52 has no fractional part to round;Floorreturns it as is. Forlongvalues above 2^53, converting todoublefirst loses digits. - Negative zero.
Math.Ceiling(-0.5)returns-0, which prints as-0and equals0. - Related names.
MathF.Flooris forfloat. Generic math exposesdouble.FloorandTNumber.Flooras static members. - Casting.
(int)xtruncates and is the fastest way to drop a fraction when you already know the range. UseFloorwhen negative inputs must round down.
Related
Math.Round(),Mathclass,Math.DivRem(),Math.Clamp()- Lessons: Math and numbers, Operators