Skip to content
Related Articles

Related Articles

C# | Math.Ceiling() Method
  • Last Updated : 31 Jan, 2019

In C#, Math.Ceiling() is a Math class method. This method is used to find the smallest integer , which is greater than or equal to the passed argument. The Celing method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it.

  • Math.Ceiling(Decimal) Method
  • Math.Ceiling(Double) Method

Math.Ceiling(Decimal) Method

This method is used to returns the smallest integral value which is greater than or equal to the specified decimal number in the argument list.

Syntax:

public static decimal Ceiling(decimal d)

Parameter:

Decimal d: It is the decimal number of type System.Decimal.



Return Type: This function return the smallest integral value which will be greater than or equal to d. The type of this method is System.Decimal and return a decimal instead of an integral type.

Examples:

Input  : 888.765M;
Output : 889

Input  : -20002.999M
Output : -20002

Program : To demonstrate the Math.Ceiling(Decimal) method.




// C# program to illustrate the
// Math.Ceiling(Decimal) function
using System;
  
class SudoPlacement {
  
    // Main method
    static void Main()
    {
  
        // Input decimal value.
        decimal decim_n1 = 2.10M;
        decimal decim_n2 = -99.90M;
        decimal decim_n3 = 33.001M;
  
        // Calculate Ceiling values by
        // Using Math.Ceiling() function
        decimal ceil_t1 = Math.Ceiling(decim_n1);
        decimal ceil_t2 = Math.Ceiling(decim_n2);
        decimal ceil_t3 = Math.Ceiling(decim_n3);
  
        // Print First values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n1);
        Console.WriteLine("Ceiling value = " + ceil_t1);
  
        // Print Second values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n2);
        Console.WriteLine("Ceiling value = " + ceil_t2);
  
        // Print third values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n3);
        Console.WriteLine("Ceiling value = " + ceil_t3);
    }
}
Output:
Input Value  = 2.10
Ceiling value = 3
Input Value  = -99.90
Ceiling value = -99
Input Value  = 33.001
Ceiling value = 34

Math.Ceiling(Double) Method

This method is used to returns the smallest integral value which is greater than or equal to the specified double-precision floating-point number in the argument list.

Syntax:

public static double Ceiling(double d)

Parameter:

Double d: It is the double number of type System.Double.

Return Type: This method returns the smallest integral value that is greater than or equal to d. If d is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned. The type of this method is System.Double.

Examples:

Input  : 10.1  
Output : 11

Input  : -2222.2220
Output : -2222

Program : To demonstrate the Math.Ceiling(Double) method.




// C# program to illustrate the
// Math.Ceiling(Double) function
using System;
  
class SudoPlacement {
  
    // Main method
    static void Main()
    {
  
        // Input different Double value.
        double n1 = 101.10;
        double n2 = -1.1;
        double n3 = 9222.1000;
  
        // Calculate Ceiling values by
        // Using Math.Ceiling() function
        double t1 = Math.Ceiling(n1);
        double t2 = Math.Ceiling(n2);
        double t3 = Math.Ceiling(n3);
  
        // Print First values and Ceiling
        Console.WriteLine("Input Value  = " + n1);
        Console.WriteLine("Ceiling value = " + t1);
  
        // Print Second values and Ceiling
        Console.WriteLine("Input Value  = " + n2);
        Console.WriteLine("Ceiling value = " + t2);
  
        // Print third values and Ceiling
        Console.WriteLine("Input Value  = " + n3);
        Console.WriteLine("Ceiling value = " + t3);
    }
}
Output:
Input Value  = 101.1
Ceiling value = 102
Input Value  = -1.1
Ceiling value = -1
Input Value  = 9222.1
Ceiling value = 9223

References:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :