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
Decimal.Ceiling() Method in C#
The Decimal.Ceiling() method in C# returns the smallest integral value that is greater than or equal to the specified decimal number. This method rounds up to the nearest whole number, regardless of whether the decimal portion is less than or greater than 0.5.
Syntax
Following is the syntax −
public static decimal Ceiling(decimal val);
Parameters
val ? A decimal number that needs to be rounded up to the nearest integer.
Return Value
Returns a decimal value representing the smallest integer greater than or equal to the input value.
Using Decimal.Ceiling() with Positive Numbers
using System;
public class Demo {
public static void Main() {
decimal val1 = 12.85m;
decimal val2 = 3.45m;
decimal val3 = 7.0m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("Ceiling (val1) = " + Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = " + Decimal.Ceiling(val2));
Console.WriteLine("Ceiling (val3) = " + Decimal.Ceiling(val3));
}
}
The output of the above code is −
Decimal 1 = 12.85 Decimal 2 = 3.45 Decimal 3 = 7.0 Ceiling (val1) = 13 Ceiling (val2) = 4 Ceiling (val3) = 7
Using Decimal.Ceiling() with Negative Numbers
using System;
public class Demo {
public static void Main() {
decimal val1 = -10.85m;
decimal val2 = -33.45m;
decimal val3 = -5.0m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("Ceiling (val1) = " + Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = " + Decimal.Ceiling(val2));
Console.WriteLine("Ceiling (val3) = " + Decimal.Ceiling(val3));
}
}
The output of the above code is −
Decimal 1 = -10.85 Decimal 2 = -33.45 Decimal 3 = -5.0 Ceiling (val1) = -10 Ceiling (val2) = -33 Ceiling (val3) = -5
Practical Example with Mixed Values
using System;
public class Demo {
public static void Main() {
decimal[] values = {4.2m, -4.2m, 0.0m, 15.99m, -7.01m};
Console.WriteLine("Original Value\tCeiling Result");
Console.WriteLine("=================================");
foreach (decimal val in values) {
Console.WriteLine($"{val}\t\t{Decimal.Ceiling(val)}");
}
}
}
The output of the above code is −
Original Value Ceiling Result ================================= 4.2 5 -4.2 -4 0.0 0 15.99 16 -7.01 -7
Conclusion
The Decimal.Ceiling() method always rounds up towards positive infinity, returning the smallest integer greater than or equal to the input value. For positive numbers, it rounds away from zero, while for negative numbers, it rounds towards zero.
