Rounding off a positive number to the next nearest multiple of 5

前端 未结 7 747
我寻月下人不归
我寻月下人不归 2020-12-11 03:39

I need to round of a number (the input is guaranteed to be an integer & is positive) to the next multiple of 5.

I tried this:

int round = ((grade         


        
7条回答
  •  渐次进展
    2020-12-11 03:53

    This is my solution using cmath::abs

    int rounded = n + abs((n % denom) - denom);
    

    You can change the denom with any other denomination you want

提交回复
热议问题