C# Math.Ceiling() – Examples

In this tutorial, we will learn about the C# Math.Ceiling() method, and learn how to use this method to find the smallest integral value that is greater than or equal to the specified double or decimal number, with the help of examples.

Ceiling(Decimal)

Math.Ceiling(d) returns the smallest integral value that is greater than or equal to the specified decimal number d.

Syntax

The syntax of Ceiling(Decimal) method is

</>
Copy
Math.Ceiling(Decimal d)

where

ParameterDescription
dA decimal number whose Ceiling value has to be found.

Return Value

The method returns value.

Example 1 – Ceiling(x)

In this example, we will some decimal values and find its ceiling value using Math.Ceiling() method.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        Decimal x, result;

        x = 12.56M;
        result = Math.Ceiling(x);
        Console.WriteLine($"Ceiling({x}) = {result}");

        x = 10M;
        result = Math.Ceiling(x);
        Console.WriteLine($"Ceiling({x}) = {result}");

        x = 10.0000001M;
        result = Math.Ceiling(x);
        Console.WriteLine($"Ceiling({x}) = {result}");
    }
}

Output

Ceiling(12.56) = 13
Ceiling(10) = 10
Ceiling(10.0000001) = 11

Ceiling(Double)

Math.Ceiling(d) returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number d.

Syntax

The syntax of Ceiling(Double) method is

</>
Copy
Math.Ceiling(Double d)

where

ParameterDescription
dA double-precision floating-point number whose Ceiling value has to be found.

Return Value

The method returns value.

Example 2 – Ceiling(x)

In this example, we will some double-precision floating-point number and find its ceiling value using Math.Ceiling() method.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        Double x, result;

        x = 12.56;
        result = Math.Ceiling(x);
        Console.WriteLine($"Ceiling({x}) = {result}");

        x = 10;
        result = Math.Ceiling(x);
        Console.WriteLine($"Ceiling({x}) = {result}");
    }
}

Output

Ceiling(12.56) = 13
Ceiling(10) = 10

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.Ceiling() method, and also learnt how to use this method with the help of C# example programs.