EN
C# / .NET - Math.Round() method example
0 points
Math.Round
is a static method that returns a number that is rounded to the closest integer number.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Round( 5.0 )); // 5
8
9
Console.WriteLine( Math.Round( 2.49 )); // 2
10
Console.WriteLine( Math.Round( 2.50 )); // 3
11
Console.WriteLine( Math.Round( 2.51 )); // 3
12
13
Console.WriteLine( Math.Round(-2.49 )); // -2
14
Console.WriteLine( Math.Round(-2.50 )); // -2
15
Console.WriteLine( Math.Round(-2.51 )); // -3
16
17
Console.WriteLine( Math.Round( 0.999 )); // 1
18
Console.WriteLine( Math.Round( 1.001 )); // 1
19
Console.WriteLine( Math.Round(-1.001 )); // -1
20
}
21
}
xxxxxxxxxx
1
namespace System
2
{
3
public static class Math
4
{
5
// ...
6
public static decimal Round(decimal d) { ... }
7
public static decimal Round(decimal d, int precision) { ... }
8
public static decimal Round(decimal d, int precision, MidpointRounding mode) { ... }
9
public static decimal Round(decimal d, MidpointRounding mode) { ... }
10
public static double Round(double value) { ... }
11
public static double Round(double value, int precision) { ... }
12
public static double Round(double value, int precision, MidpointRounding mode) { ... }
13
public static double Round(double value, MidpointRounding mode) { ... }
14
// ...
15
}
16
}
Syntax | Syntax example is above. |
Parameters |
|
Result | Rounded number value (primitive value). |
Description | Round is a static method that returns a number that is rounded to the closest integer number. |
This section contains a custom function that shows how to round number with precision to n
places.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double RoundPrecised(double number, int precision)
6
{
7
double power = Math.Pow(10, precision);
8
9
return Math.Round(number * power) / power;
10
}
11
12
public static void Main(string[] args)
13
{
14
Console.WriteLine( RoundPrecised( 5 , 0 ) ); // 5
15
Console.WriteLine( RoundPrecised( 5.0, 0 ) ); // 5
16
Console.WriteLine( RoundPrecised( 0.5, 0 ) ); // 0
17
18
Console.WriteLine( RoundPrecised( 1.2345, 0 ) ); // 1
19
Console.WriteLine( RoundPrecised( 1.2345, 1 ) ); // 1.2
20
Console.WriteLine( RoundPrecised( 1.2345, 2 ) ); // 1.23
21
Console.WriteLine( RoundPrecised( 1.2345, 3 ) ); // 1.234
22
23
Console.WriteLine( RoundPrecised( -1.2345, 0 ) ); // -1
24
Console.WriteLine( RoundPrecised( -1.2345, 1 ) ); // -1.2
25
Console.WriteLine( RoundPrecised( -1.2345, 2 ) ); // -1.23
26
Console.WriteLine( RoundPrecised( -1.2345, 3 ) ); // -1.234
27
28
Console.WriteLine( RoundPrecised( 12345, -1 ) ); // 12340
29
Console.WriteLine( RoundPrecised( 12345, -2 ) ); // 12300
30
Console.WriteLine( RoundPrecised( 12345, -3 ) ); // 12000
31
}
32
}
This section contains custom round function implementation.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double RoundNumber(double value)
6
{
7
if (value < 0.0)
8
{
9
double rest = (value % 1.0);
10
11
if (rest < -0.5)
12
{
13
rest += 1.0;
14
}
15
16
return value - rest;
17
}
18
else
19
{
20
value += 0.5;
21
22
return value - (value % 1.0);
23
}
24
}
25
26
public static void Main(string[] args)
27
{
28
// Example:
29
30
Console.WriteLine( RoundNumber( 5 ) ); // 5
31
32
Console.WriteLine( RoundNumber( 2.49 ) ); // 2
33
Console.WriteLine( RoundNumber( 2.50 ) ); // 3
34
Console.WriteLine( RoundNumber( 2.51 ) ); // 3
35
36
Console.WriteLine( RoundNumber( 0.999 ) ); // 1
37
Console.WriteLine( RoundNumber( 1.001 ) ); // 1
38
39
Console.WriteLine( RoundNumber( -2.49 ) ); // -2
40
Console.WriteLine( RoundNumber( -2.50 ) ); // -2
41
Console.WriteLine( RoundNumber( -2.51 ) ); // -3
42
43
Console.WriteLine( RoundNumber( -1.001 ) ); // -1
44
}
45
}