# ASP.NET C# - Round Up When Converting Double To Integer

This rounds to the nearest number:
``````            double x = 12.3;
int n = Convert.ToInt32(x);
``````

I want to round up.  This gives an error:
``````            double x = 12.3;
int n = Math.Ceiling(x);
``````
LVL 10
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
When converting from double to Integer please look at the following examples from

https://msdn.microsoft.com/en-us/library/ffdk7eyz.aspx

``````double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98,
0, 9.113e-16, 103.919, 17834.191, Double.MaxValue };
int result;

foreach (double value in values)
{
try {
result = Convert.ToInt32(value);
Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
value.GetType().Name, value,
result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Int32 type.", value);
}
}
//    -1.79769313486232E+308 is outside the range of the Int32 type.
//    -13800000000 is outside the range of the Int32 type.
//    Converted the Double value '-1023.299' to the Int32 value -1023.
//    Converted the Double value '-12.98' to the Int32 value -13.
//    Converted the Double value '0' to the Int32 value 0.
//    Converted the Double value '9.113E-16' to the Int32 value 0.
//    Converted the Double value '103.919' to the Int32 value 104.
//    Converted the Double value '17834.191' to the Int32 value 17834.
//    1.79769313486232E+308 is outside the range of the Int32 type.
``````
Freelance programmer / ConsultantCommented:
double x = 12.3;
x += 0.5;
int n = Convert.ToInt32(x);
DeveloperCommented:
You get an error because Ceiling returns a double.  Because there is no implicit conversion between a double and an integer, you have to Cast or Convert your double to an Int32.

Using an explicit cast -
``````using System;

namespace EE_Q28672076
{
class Program
{
static void Main(string[] args)
{
double x = 12.3;
int n = (int)Math.Ceiling(x);
Console.WriteLine("We started out as {0} but ended up at {1}", x, n);
}
}
}
``````
Or using the Convert.ToInt32() method -
``````using System;

namespace EE_Q28672076
{
class Program
{
static void Main(string[] args)
{
double x = 12.3;
int n = Convert.ToInt32(Math.Ceiling(x));
Console.WriteLine("We started out as {0} but ended up at {1}", x, n);
}
}
}
``````
Both of the above produce the following output --saige-

Experts Exchange Solution brought to you by