Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Ceiling() Method in C#
The Math.Ceiling() method in C# returns the smallest integer value that is greater than or equal to the specified number. This method always rounds up to the next whole number, making it useful for scenarios where you need to ensure a minimum integer value.
Syntax
The Math.Ceiling() method has two overloads −
public static decimal Ceiling(decimal val); public static double Ceiling(double val);
Parameters
-
val − The decimal or double number to be rounded up to the nearest integer.
Return Value
Returns the smallest integer value greater than or equal to the input value. The return type matches the input type (decimal or double).
Using Math.Ceiling() with Decimal Values
Example
using System;
public class Demo {
public static void Main() {
decimal val1 = 9.99M;
decimal val2 = -5.10M;
decimal val3 = 15.0M;
Console.WriteLine("Ceiling(9.99) = " + Math.Ceiling(val1));
Console.WriteLine("Ceiling(-5.10) = " + Math.Ceiling(val2));
Console.WriteLine("Ceiling(15.0) = " + Math.Ceiling(val3));
}
}
The output of the above code is −
Ceiling(9.99) = 10 Ceiling(-5.10) = -5 Ceiling(15.0) = 15
Using Math.Ceiling() with Double Values
Example
using System;
public class Demo {
public static void Main() {
double val1 = 3.1;
double val2 = 55.99;
double val3 = -55.99;
double val4 = 0.1;
Console.WriteLine("Ceiling(3.1) = " + Math.Ceiling(val1));
Console.WriteLine("Ceiling(55.99) = " + Math.Ceiling(val2));
Console.WriteLine("Ceiling(-55.99) = " + Math.Ceiling(val3));
Console.WriteLine("Ceiling(0.1) = " + Math.Ceiling(val4));
}
}
The output of the above code is −
Ceiling(3.1) = 4 Ceiling(55.99) = 56 Ceiling(-55.99) = -55 Ceiling(0.1) = 1
Common Use Cases
The Math.Ceiling() method is commonly used when you need to calculate minimum quantities, such as determining how many containers are needed to hold a certain number of items, or rounding up prices to the next whole unit.
Example
using System;
public class Demo {
public static void Main() {
// Calculate minimum pages needed for a document
double totalItems = 157;
double itemsPerPage = 20;
double pagesNeeded = totalItems / itemsPerPage;
Console.WriteLine("Items per page: " + itemsPerPage);
Console.WriteLine("Total items: " + totalItems);
Console.WriteLine("Exact pages needed: " + pagesNeeded);
Console.WriteLine("Minimum pages required: " + Math.Ceiling(pagesNeeded));
}
}
The output of the above code is −
Items per page: 20 Total items: 157 Exact pages needed: 7.85 Minimum pages required: 8
Conclusion
The Math.Ceiling() method in C# always rounds numbers up to the next integer, making it essential for scenarios requiring minimum whole number calculations. It works with both decimal and double data types, consistently rounding toward positive infinity regardless of whether the input is positive or negative.
