C# Math Ceiling() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Ceiling(value) rounds a number up to the nearest whole number. Math.Ceiling(2.1) is 3. A number that is already whole stays the same.

Think of the ceiling of a room, above your head. Anything with even a small decimal part goes up to the next whole number. This is exactly what you need when counting how many containers you need: 25 items in boxes of 10 need 3 boxes, not 2.5. It is the opposite of Floor(). "Up" means toward positive infinity, so Math.Ceiling(-2.9) is -2.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Ceiling(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Ceiling is in every .NET version, with double and decimal overloads.

Syntax

Pass a double or decimal. You get the same type back, holding a whole number.

C#
double a = Math.Ceiling(2.1);     // 3
double b = Math.Ceiling(-2.9);    // -2
decimal c = Math.Ceiling(7.1m);   // 8
int boxes = (int)Math.Ceiling(25 / 10.0);   // 3
CallWhat it does
Math.Ceiling(2.1)Returns 3.
Math.Ceiling(2.0)Returns 2. Whole numbers do not change.
Math.Ceiling(-2.9)Returns -2, the next whole number above.
Math.Ceiling(double.NaN)Returns NaN.

The result is a double (or decimal), so cast when you need an int. When you divide two int values, C# does integer division first and the decimals are already gone, so 25 / 10 is already 2 before Ceiling sees it. Make one side a double: 25 / 10.0.

Library declarations
C#
public static double Ceiling(double a);
public static decimal Ceiling(decimal d);

Parameters

ParameterDescription
a / dRequired. The double or decimal number to round up.

Return Value

Return ValueDescription
double or decimalThe smallest whole number that is greater than or equal to the input. NaN and infinities are returned unchanged.

C# Examples

Example 1 — Round Up to a Whole Number

Call Ceiling on numbers with small and large decimal parts, a whole number, and a negative.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Ceiling(2.1));
        Console.WriteLine(Math.Ceiling(2.9));
        Console.WriteLine(Math.Ceiling(3.0));
        Console.WriteLine(Math.Ceiling(-2.9));
    }
}

How It Works

  1. Small decimal part. 2.1 is just above 2, but Ceiling still goes up to 3. Any decimal part is enough.
  2. Large decimal part. 2.9 also goes up to 3. Ceiling does not look at how close the number is.
  3. Already whole. 3.0 is not above 3, so the answer stays 3.
  4. Negative. -2.9 sits between -3 and -2. The higher one on the number line is -2.

Example 2 — How Many Boxes Do I Need?

Divide items by the box size using a double, and round up so a part-filled box still counts.

C#
using System;

class Program {
    static void Main() {
        int perBox = 10;
        int[] items = { 25, 30, 31 };

        foreach (int count in items) {
            int boxes = (int)Math.Ceiling(count / (double)perBox);
            Console.WriteLine(boxes);
        }

        double wrong = 25 / 10;
        Console.WriteLine(Math.Ceiling(wrong));
    }
}

How It Works

  1. Cast one side. (double)perBox turns the division into a decimal division. 25 / 10.0 is 2.5, not 2.
  2. 25 items. 2.5 boxes rounds up to 3, because the last 5 items still need a box.
  3. 30 and 31 items. 30 / 10 is exactly 3.0, so you need 3. 31 / 10 is 3.1, which goes up to 4.
  4. The trap. 25 / 10 uses two int values, so integer division gives 2 before the result is stored in wrong. Ceiling of 2 is 2, which is one box too few.

Example 3 — Floor, Ceiling, and decimal Together

See how Ceiling and Floor split a positive and a negative number, and use decimal overloads.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Ceiling(2.5));
        Console.WriteLine(Math.Floor(2.5));
        Console.WriteLine(Math.Ceiling(-2.5));
        Console.WriteLine(Math.Floor(-2.5));

        Console.WriteLine(Math.Ceiling(7.1m));
        Console.WriteLine(Math.Ceiling(-7.9m));
    }
}

How It Works

  1. Positive half. 2.5 goes up to 3 with Ceiling and down to 2 with Floor. The two always sit one apart unless the number is already whole.
  2. Negative half. -2.5 goes up to -2 with Ceiling and down to -3 with Floor. Up means toward the positive side.
  3. decimal up. Math.Ceiling(7.1m) is 8. The decimal overload behaves just like the double one.
  4. decimal negative. Math.Ceiling(-7.9m) is -7, the whole number above -7.9.

Frequently Asked Questions

It rounds a number up to the nearest whole number. Math.Ceiling(2.1) is 3 and Math.Ceiling(-2.9) is -2. The result is a double or decimal, not an int.
Divide with a double and use Ceiling: (int)Math.Ceiling(count / (double)size). With only int values, the decimals are lost before Ceiling runs (and Math.Ceiling does not even accept a plain int without a cast).
Round goes to the nearest whole number (3.2 becomes 3). Ceiling always goes up (3.2 becomes 4). They only agree when the decimals are .5 or more, with Round's halfway rule aside.
Yes. It rounds toward positive infinity, so negatives get closer to zero: Math.Ceiling(-2.9) is -2.
No. Math is a static class, so you call Math.Ceiling(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Clamp()

Keep a value between a minimum and a maximum.

Clamp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful