Round a Double Value to an Integer Value in C#
-
Round a Double Value to an Integer Value With the
Math.Ceiling()
Function inC#
-
Round a Double Value to an Integer Value With the
Math.Floor()
Function inC#
-
Round a Double Value to an Integer Value With the
Math.Round()
Function inC#

This tutorial will discuss the methods to round up a double value to an integer value in C#.
Round a Double Value to an Integer Value With the Math.Ceiling()
Function in C#
If we want to round up the double value 2.5
to the integer value 3
, we have to use the Math.Ceiling()
function. The Math.Ceiling()
function rounds up a decimal value to the next integer value. The following code example shows us how we can round up a double value to an integer value with the Math.Ceiling()
function in C#.
using System;
namespace round_double_to_intt
{
class Program
{
static void Main(string[] args)
{
double d = 2.5;
int i = (int)Math.Ceiling(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
Output:
Original Value = 2.5
Rounded Value = 3
We rounded up the double value 2.5
to the integer value 3
with the Math.Ceiling()
function in C#. The problem with this approach is that the Math.Ceiling()
function converts the decimal value 2.3
to the integer value 3
.
Round a Double Value to an Integer Value With the Math.Floor()
Function in C#
If we want to round up the double value 2.5
to the integer value 2
, we have to use the Math.Floor()
function. The Math.Floor()
function rounds up a decimal value to the previous integer value. The following code example shows us how we can round up a double value to an integer value with the Math.Floor()
function in C#.
using System;
namespace round_double_to_intt
{
class Program
{
static void Main(string[] args)
{
double d = 2.5;
int i = (int)Math.Floor(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
Output:
Original Value = 2.5
Rounded Value = 2
We rounded up the double value 2.5
to the integer value 2
with the Math.Floor()
function in C#. The problem with this approach is that the Math.Floor()
function converts the decimal value 2.9
to the integer value 2
.
Round a Double Value to an Integer Value With the Math.Round()
Function in C#
The Math.Round()
function can be used to round up a double value to the nearest integer value in C#. The Math.Round()
function returns a double value that is rounded up to the nearest integer. The following code example shows us how we can round up a double value to an integer value with the Math.Round()
function in C#.
using System;
namespace round_double_to_intt
{
class Program
{
static void Main(string[] args)
{
double d = 2.9;
int i = (int)Math.Round(d);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
Output:
Original Value = 2.9
Rounded Value = 3
We rounded up the decimal value 2.9
to the integer value 3
with the Math.Round()
function in C#. We used typecasting to convert the double value returned by the Math.Round()
function to an integer value. There is only one problem with this approach. The Math.Round()
function converts the decimal value 2.5
to the integer value 2
.
We can fix this problem by specifying MidpointRounding.AwayFromZero
in the arguments of the Math.Round()
function. The following code example shows us how we can round up 2.5
to 3
with the Math.Round()
function in C#.
using System;
namespace round_double_to_intt
{
class Program
{
static void Main(string[] args)
{
double d = 2.5;
int i = (int)Math.Round(d, MidpointRounding.AwayFromZero);
Console.WriteLine("Original Value = {0}", d);
Console.WriteLine("Rounded Value = {0}", i);
}
}
}
Output:
Original Value = 2.5
Rounded Value = 3
We rounded up the decimal value 2.5
to the integer value 3
by specifying the MidpointRounding.AwayFromZero
parameter in the Math.Round()
function in C#.
All the methods discussed above are useful in different specific scenarios. The worst possible way to round up a double value to an integer value is by explicit typecasting. This is because the explicit typecasting ignores all the values after the decimal point and just returns the integer value before the decimal point.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn