ZetCode

C# round

last modified July 5, 2023

In this article we show how to round numbers in C#.

Rounding numbers is adjusting the digits up or down to make calculations easier.

C# rounding methods

Basic rounding can be done with Ceiling and Floor methods. The Ceiling returns the smallest integral value that is greater than or equal to the specified number, while the Floor method returns the largest integral value that is less than or equal to the specified decimal number.

More complex rounding is done with the Round method. There are multiple overloaded Round methods. We can use the Math.Round, float.Round, double.Round, or decimal.Round methods to do the rounding.

Math.Round(decimal d, int decimals, MidpointRounding mode)

This is one of the overloaded methods that accepts a decimal value. The method rounds a decimal value to a specified number of fractional digits using the specified rounding mode.

ModeDescription
AwayFromZerorounded toward the nearest number that's away from zero
ToEvenrounded toward the nearest even number
ToNegativeInfinitydownwards-directed rounding
ToPositiveInfinityupwards-directed rounding
ToZerorounding towards zero

The table shows the rounding modes available.

C# Ceiling & Floor

In the first example, we round double values with Ceiling and Floor.

Program.cs
double n1 = 1.467;

Console.WriteLine(Math.Ceiling(n1));
Console.WriteLine(Math.Floor(n1));

Console.WriteLine(Math.Ceiling(-n1));
Console.WriteLine(Math.Floor(-n1));

double n3 = 6.967;

Console.WriteLine(double.Ceiling(n3));
Console.WriteLine(double.Floor(n3));
Console.WriteLine(double.Ceiling(-n3));
Console.WriteLine(double.Floor(-n3));

We round two double values. The output is always an integer.

Console.WriteLine(Math.Ceiling(n1));
Console.WriteLine(Math.Floor(n1));

We use the methods from the Math class.

Console.WriteLine(double.Ceiling(n3));
Console.WriteLine(double.Floor(n3));

We can also use the methods from the double type.

$ dotnet run
2
1
-1
-2
7
6
-6
-7

C# Round fractional digits

We can specify the number of fractional digits to which we round the number.

Program.cs

float n1 = 1.23487f;
float n2 = 3.97451f;

Console.WriteLine(n1);
Console.WriteLine(n2);

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4));
Console.WriteLine(Math.Round(n2, 4));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 3));
Console.WriteLine(Math.Round(n2, 3));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 2));
Console.WriteLine(Math.Round(n2, 2));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 1));
Console.WriteLine(Math.Round(n2, 1));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 0));
Console.WriteLine(Math.Round(n2, 0));

In the program, we have two floats. We round them to four, three, two, one, and zero fractional digits with Math.Round.

$ dotnet run
1.23487
3.97451
-------------------------
1.2349
3.9745
-------------------------
1.235
3.975
-------------------------
1.23
3.97
-------------------------
1.2
4
-------------------------
1
4

C# rounding modes

In the next example, we demonstrate the various rounding modes.

Program.cs
double n1 = 1/7d;
double n2 = -1/7d;

Console.WriteLine(n1);
Console.WriteLine(n2);

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4, MidpointRounding.ToEven));
Console.WriteLine(Math.Round(n2, 4, MidpointRounding.ToEven));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4, MidpointRounding.AwayFromZero));
Console.WriteLine(Math.Round(n2, 4, MidpointRounding.AwayFromZero));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4, MidpointRounding.ToZero));
Console.WriteLine(Math.Round(n2, 4, MidpointRounding.ToZero));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4, MidpointRounding.ToNegativeInfinity));
Console.WriteLine(Math.Round(n2, 4, MidpointRounding.ToNegativeInfinity));

Console.WriteLine("-------------------------");

Console.WriteLine(Math.Round(n1, 4, MidpointRounding.ToPositiveInfinity));
Console.WriteLine(Math.Round(n2, 4, MidpointRounding.ToPositiveInfinity));

In the example, we have a positive and a negative double value. We round the values using the available rounding modes to four fractional digits.

$ dotnet run
0.14285714285714285
-0.14285714285714285
-------------------------
0.1429
-0.1429
-------------------------
0.1429
-0.1429
-------------------------
0.1428
-0.1428
-------------------------
0.1428
-0.1429
-------------------------
0.1429
-0.1428p

C# rounding example

In the next example, we round a decimal number using various rounding modes and 1..5 fractional digits.

Program.cs
using System.Text;

decimal n = 1 / 7m;
// decimal n = -1/7m;

char s = ' ';
string cln = new string('-', 10);

string h1 = "(1)";
string h2 = "(2)";
string h3 = "(3)";
string h4 = "(4)";
string h5 = "(5)";

var precisions = new List<int> { 1, 2, 3, 4, 5 };
var modes = new List<MidpointRounding>
{
    MidpointRounding.ToEven, MidpointRounding.ToZero,
    MidpointRounding.AwayFromZero, MidpointRounding.ToNegativeInfinity,
    MidpointRounding.ToPositiveInfinity
};

Console.WriteLine($"{s,20}{h1,-10}{h2,-10}{h3,-10}{h4,-10}{h5,-10}");
Console.WriteLine($"{s,20}{cln,10}{cln,10}{cln,10}{cln,10}{cln,10}");

foreach (var mode in modes)
{
    var builder = new StringBuilder();
    builder.Append($"{mode,-20}");

    foreach (var prec in precisions)
    {
        builder.Append($"{decimal.Round(n, prec, mode),-10}");
    }

    Console.WriteLine(builder.ToString());
}

The output is shown in a neat table.

$ dotnet run
                    (1)       (2)       (3)       (4)       (5)
                    --------------------------------------------------
ToEven              0.1       0.14      0.143     0.1429    0.14286
ToZero              0.1       0.14      0.142     0.1428    0.14285
AwayFromZero        0.1       0.14      0.143     0.1429    0.14286
ToNegativeInfinity  0.1       0.14      0.142     0.1428    0.14285
ToPositiveInfinity  0.2       0.15      0.143     0.1429    0.14286

This is the output for the positive value.

$ dotnet run
                    (1)       (2)       (3)       (4)       (5)
                    --------------------------------------------------
ToEven              -0.1      -0.14     -0.143    -0.1429   -0.14286
ToZero              -0.1      -0.14     -0.142    -0.1428   -0.14285
AwayFromZero        -0.1      -0.14     -0.143    -0.1429   -0.14286
ToNegativeInfinity  -0.2      -0.15     -0.143    -0.1429   -0.14286
ToPositiveInfinity  -0.1      -0.14     -0.142    -0.1428   -0.14285

This is the output for the negative value.

Source

Math.Round method

In this article we have shown how to round numbers in C#.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all C# tutorials.