.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

**Math.Ceiling** rounds up to the next full integer. This means that any number over 1 but under 2 would be rounded to 2. This is not the same result from rounding a number. Math.Ceiling is considered a ceiling function in mathematics.

**Example.** First, the Math.Ceiling method in the System namespace is a static method that returns a value type. The method receives a double or decimal type which is resolved at compile-time.

**Then:**It performs the computation and returns a new value as the result type at runtime.

**The method** copies the actual values and no allocations are done internally. This example shows that the number 123.456 has the ceiling of 124. We compute this with Math.Ceiling and display the result with Console.WriteLine.

C# program that uses Ceilingusing System; class Program { static void Main() {// Get ceiling of double value.double value1 = 123.456; double ceiling1 =Math.Ceiling(value1);// Get ceiling of decimal value.decimal value2 = 456.789M; decimal ceiling2 =Math.Ceiling(value2);// Get ceiling of negative value.double value3 = -100.5; double ceiling3 =Math.Ceiling(value3);// Write values.Console.WriteLine(value1); Console.WriteLine(ceiling1); Console.WriteLine(value2); Console.WriteLine(ceiling2); Console.WriteLine(value3); Console.WriteLine(ceiling3); } }Output123.456 124 456.789 457 -100.5 -100

**In this example,** different versions of Math.Ceiling are called because the compiler applies overload resolution each time. The ceiling of the number 123.456 is 124. The decimal type with value 456.789 has a ceiling of 457.

**Negative ceiling values.** When you call the Math.Ceiling method on a negative floating point type, the number will be also be rounded up, which means that the ceiling of -100.5 is -100.

**Note:**For negative numbers, the digits after the decimal place are removed but the final result is not incremented.

**Implementation.** We review the internal implementation of the Math.Ceiling method in the base class library. The overload for Math.Ceiling that acts on double types will call into a hidden method that is not represented in managed code.

**So:**Math.Ceiling is likely to be far more optimized than any other method you could develop in C# code.

**For decimal,** the decimal.Ceiling method is invoked, which calls into the explicit unary negation operator on the decimal type. The ceiling is found by negating the value, taking its floor, and then negating the result again.

**Uses.** Here we discuss some usages of the Math.Ceiling and Math.Floor methods in the C# language. When you are reporting percentages based on data, using Math.Ceiling or Math.Floor is useful—they can help make the output more consistent.

**Also,** when you are trying to display numbers that together represent another number, carefully controlling rounding with Ceiling and Floor is useful. This is because it avoids errors.

**Summary.** We looked at the functionality of the Math.Ceiling method. This function always rounds up a number, even when used on a negative number. We noted the implementation and some possible uses of the ceiling and floor functions in software.