Answered by:
Using the ceiling method
Question

I'm looking to do a basic ceiling operation, such that my integer will round up to the nearest 60, i can't seem to get it working. My code is:
int first = Math.Ceiling(119),60;
Console.WriteLine(first);It's telling me the call is ambiguous between decimal and double, but I'm only working with integers.
Any advice would be appreciated,
thanks
Answers

Ah, OK, this will work then:
j = 119;
while(true) { Math.DivRem(j, 60, out int rem); if (rem == 0) break; j++; }
~~Bonnie DeWitt [C# MVP]
http://geekgoddessbonnie.blogspot.com
 Edited by BonnieBMVP, Moderator Sunday, January 6, 2019 6:11 PM
 Proposed as answer by Andrew B. Painter Sunday, January 6, 2019 6:15 PM
 Marked as answer by matty1248 Sunday, January 6, 2019 6:31 PM
All replies

Hi Matty,
What you have there shouldn't even compile (that ,60 is wrong).
What you want it to do is not the way Math.Ceiling() works anyway ... Ceiling() takes a double or decimal number and returns the next higher integral number. So, if you had Math.Ceiling(119.3) it would return 120.0
Am I correct in assuming that you want 119 to "round up" to 160?
There's probably no way to do that other than a "brute force" methodology. I've been thinking, but haven't come up with an easy way. I'll think on it some more ...
~~Bonnie DeWitt [C# MVP]

Hi Bonnie,
thanks for your response, i think because ceiling in Excel works differently it's causing me some confusion.
You are right, it wont compile! I wrote that up just to demonstrate the problem that i'm having. I'm making a canvas based app in UWP and i want it such that the user can input a number and it will round it up to the nearest 60, from 0. In this case i was wanting a return value of 120, and any value would similarly be rounded to the nearest increment of 60. So typical return values would be 60, 120, 180, 240 and so on.
Thanks again,
:)

Ah, OK, this will work then:
j = 119;
while(true) { Math.DivRem(j, 60, out int rem); if (rem == 0) break; j++; }
~~Bonnie DeWitt [C# MVP]
http://geekgoddessbonnie.blogspot.com
 Edited by BonnieBMVP, Moderator Sunday, January 6, 2019 6:11 PM
 Proposed as answer by Andrew B. Painter Sunday, January 6, 2019 6:15 PM
 Marked as answer by matty1248 Sunday, January 6, 2019 6:31 PM


No problem, Matthew! Glad I could help! =0)
~~Bonnie DeWitt [C# MVP]