C# Tutorial C# Advanced C# References

C# Math - Ceiling() Method



The C# Ceiling() method returns the next highest integer value by rounding up the specified number, if necessary. In other words, it rounds the fraction UP of the given number. The method can be overloaded and it can take decimal and double arguments. In special cases it returns the following:

  • If the argument value is already an integer, the method returns the same as the argument.
  • If the argument is NaN or positive infinity or negative infinity, the method returns the same as the argument.

Syntax

public static decimal Ceiling (decimal arg);
public static double Ceiling (double arg); 

Parameters

arg Specify a number.

Return Value

Returns the next highest integer value by rounding UP the specified number, if necessary.

Example:

In the example below, Ceiling() method is used to round the fraction UP of the specified number.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.Ceiling(10.5) = "
                      + Math.Ceiling(10.5));
    Console.WriteLine("Math.Ceiling(-10.5) = "
                      + Math.Ceiling(-10.5));
    Console.WriteLine("Math.Ceiling(0.5) = "
                      + Math.Ceiling(0.5));
    Console.WriteLine("Math.Ceiling(-0.5) = "
                      + Math.Ceiling(-0.5));
    Console.WriteLine("Math.Ceiling(Double.NaN) = "
                      + Math.Ceiling(Double.NaN));
    Console.WriteLine("Math.Ceiling(Double.NegativeInfinity) = "
                      + Math.Ceiling(Double.NegativeInfinity));
    Console.WriteLine("Math.Ceiling(Double.PositiveInfinity) = "
                      + Math.Ceiling(Double.PositiveInfinity));
  }
}

The output of the above code will be:

Math.Ceiling(10.5) = 11
Math.Ceiling(-10.5) = -10
Math.Ceiling(0.5) = 1
Math.Ceiling(-0.5) = 0
Math.Ceiling(Double.NaN) = NaN
Math.Ceiling(Double.NegativeInfinity) = -Infinity
Math.Ceiling(Double.PositiveInfinity) = Infinity

❮ C# Math Methods

5