EN
C# / .NET - Math.Ceiling() method example
0 points
The Math.Ceiling()
function returns an integer value that is greater than or equal to the argument - the result of round-up operation.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Ceiling( 5.0 )); // 5
8
9
Console.WriteLine( Math.Ceiling( 2.49 )); // 3
10
Console.WriteLine( Math.Ceiling( 2.50 )); // 3
11
Console.WriteLine( Math.Ceiling( 2.51 )); // 3
12
13
Console.WriteLine( Math.Ceiling( -2.49 )); // -2
14
Console.WriteLine( Math.Ceiling( -2.50 )); // -2
15
Console.WriteLine( Math.Ceiling( -2.51 )); // -2
16
17
Console.WriteLine( Math.Ceiling( 0.999 )); // 1
18
Console.WriteLine( Math.Ceiling( 1.001 )); // 2
19
Console.WriteLine( Math.Ceiling( -1.001 )); // -1
20
}
21
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static decimal Ceiling(decimal number) { ... } 7 public static double Ceiling(double number) { ... } 8 // ... 9 } 10 } |
Parameters | number - decimal or double value (primitive value). |
Result |
Rounded up If input If input If input |
Description | Ceiling is a static method that takes only one parameter and returns a rounded-up value. |
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CeilingPrecised(double number, int precision)
6
{
7
double power = Math.Pow(10, precision);
8
return Math.Ceiling(number * power) / power;
9
}
10
11
public static void Main(string[] args)
12
{
13
Console.WriteLine( CeilingPrecised( 5 , 0 )); // 5
14
Console.WriteLine( CeilingPrecised( 5.0 , 0 )); // 5
15
Console.WriteLine( CeilingPrecised( 0.5 , 0 )); // 1
16
17
Console.WriteLine( CeilingPrecised( 1.1234, 0 )); // 2
18
Console.WriteLine( CeilingPrecised( 1.1234, 1 )); // 1.2
19
Console.WriteLine( CeilingPrecised( 1.1235, 2 )); // 1.13
20
Console.WriteLine( CeilingPrecised( 1.1235, 3 )); // 1.124
21
22
Console.WriteLine( CeilingPrecised( -1.1234, 0 )); // -1
23
Console.WriteLine( CeilingPrecised( -1.1234, 1 )); // -1.1
24
Console.WriteLine( CeilingPrecised( -1.1234, 2 )); // -1.12
25
Console.WriteLine( CeilingPrecised( -1.1234, 3 )); // -1.123
26
27
Console.WriteLine( CeilingPrecised( 1234, -1 )); // 1240
28
Console.WriteLine( CeilingPrecised( 1234, -2 )); // 1300
29
Console.WriteLine( CeilingPrecised( 1234, -3 )); // 2000
30
31
Console.WriteLine( CeilingPrecised( 5_000.000_001, 0 )); // 5001
32
Console.WriteLine( CeilingPrecised( 5_000.000_001, 6 )); // 5000.000001
33
Console.WriteLine( CeilingPrecised( 5_000.000_001, -3 )); // 6000
34
}
35
}