How do I round up a decimal in C#? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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); } } }

12th Dec 2021, 6:16 AM
Harley Wilkinson
Harley Wilkinson - avatar
4 Antworten
+ 6
we should use double for all variables instead of int. then we can do: ... Console.WriteLine(Math.Ceiling(rolls));
12th Dec 2021, 7:24 AM
Lothar
Lothar - avatar
+ 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 ...
12th Dec 2021, 7:09 AM
Ipang
+ 1
var roll = (total_area /1440); Console.WriteLine($@"{roll:F0}");
12th Dec 2021, 7:10 AM
hossein B
hossein B - avatar
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);
13th Dec 2021, 12:21 PM
Mahdi Ramin
Mahdi Ramin - avatar