Page 1 of 1
rounding in c++
Posted: Thu May 06, 2010 8:29 pm
by Randi
I am trying to round a number to the nearest 5 or 10, is there any easy way of doing this?
for example: I have the number 44, it would round to 45.
Re: rounding in c++
Posted: Thu May 06, 2010 9:05 pm
by Ginto8
Code: Select all
int roundToNrst5(int x) {
int y = x%5;
if(y>2 || y == 0)
return x+(5-y);
else
return x-y
}
% is modulus (remainder of division). Aside from that you should be able to figure it out.
Re: rounding in c++
Posted: Sun May 09, 2010 11:45 am
by Randi
Thank you very much Ginto!
Re: rounding in c++
Posted: Sun May 09, 2010 3:59 pm
by Ginto8
Randi wrote:Thank you very much Ginto!
no problem, always glad to help