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.
rounding in c++
Moderator: Coders of Rage
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: rounding in c++
Code: Select all
int roundToNrst5(int x) {
int y = x%5;
if(y>2 || y == 0)
return x+(5-y);
else
return x-y
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: rounding in c++
Thank you very much Ginto!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: rounding in c++
no problem, always glad to helpRandi wrote:Thank you very much Ginto!
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.