0
How do I round up a decimal in C#?
I'm working on the duct tape problem and most of my cases are right except one and I know the issue is because it's not rounding the number of rolls up but I'm not sure how to do that. Any help please?! using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int height = Convert.ToInt32(Console.ReadLine()) * 12; int length = Convert.ToInt32(Console.ReadLine()) * 12; int total_area = (height * length) * 2; int rolls = (total_area / 1440); Console.Write(rolls); } } }
4 Answers
+ 6
we should use double for all variables instead of int. then we can do:
...
Console.WriteLine(Math.Ceiling(rolls));
+ 4
I can only think of two methods that might do what you want
Math.Ceiling()
Math.Round()
For best result, please consult the docs to see which one does exactly what you want ...
+ 1
var roll = (total_area /1440);
Console.WriteLine($@"{roll:F0}");
0
If you mean rounding up, you can do one of the following:
In the first step :
double total_area = height * length * 2;
Then đđđ
int rolls = (int) (total_area / 1440 + 1);
// or
int rolls = Convert.ToInt32(total_area / 1440);
// or
int rolls = (int) Math.Ceiling(total_area / 1440);