C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example: We see that the number 123.456 has the ceiling of 124. We compute this with Math.Ceiling and display the result.
ConsoleInfo: 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.
DoubleDecimalNegative: 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
So: Math.Ceiling is likely to be far more optimized than any other method you could develop in C# code.