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);

Open in new window


I want to round up.  This gives an error:
            double x = 12.3;
            int n = Math.Ceiling(x);

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

plusone3055Commented:
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.

Open in new window

AndyAinscowFreelance programmer / ConsultantCommented:
How about this:
double x = 12.3;
x += 0.5;
            int n = Convert.ToInt32(x);
it_saigeDeveloperCommented:
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);
			Console.ReadLine();
		}
	}
}

Open in new window

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);
			Console.ReadLine();
		}
	}
}

Open in new window

Both of the above produce the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.