HomeSearch

C# Math.Ceiling Usage

Use the Math.Ceiling method from the System namespace to get higher integers.
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. 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.Static

Example: We see that the number 123.456 has the ceiling of 124. We compute this with Math.Ceiling and display the result.

Console

Info: Different versions of Math.Ceiling are called because the compiler applies overload resolution each time.

Result: The ceiling of the number 123.456 is 124. The decimal type with value 456.789 has a ceiling of 457.

DoubleDecimal

Negative: When you call Math.Ceiling on a negative floating point type, the number will be also be rounded up. The ceiling of -100.5 is -100.

C# program that uses Ceiling using 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); } } Output 123.456 124 456.789 457 -100.5 -100
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.Math.Floor
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.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls