简体   繁体   中英

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

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 = ((grades[j] + 2)/5) * 5;

However, this rounds off the number to the nearest multiple of 5.

Eg: 67 is rounded off to 65, not 70.

To round up the general form should be:

((n + denominator -1) / denominator )* denominator 

so in your case:

int round = ((grades[j] + 4)/5) * 5;

The reason we deduct 1 from the denominator is to handle exact multiples of the rounding value for instance:

((70 + 4) / 5) * 5

would yield 70

You can take the difference between grades[j] and the next number, and just add it. For example, if grades[j] == 12 then 12 mod 5 == 2 , so add 5 - 2 .

Here is a sample program to test it out:

#include <iostream>

int main() {
    int x[] = {2,7,123,32}; // some random numbers to show how this works
    for (int i = 0; i < 4; {
        std::cout << x[i] << "\t" << x[i] + ((5-(x[i] % 5)) % 5) << std::endl;
    }
    return 0;
}

Output:

2   5
7   10
123 125
32  35
int mod = grades[j] % 5;
int round = grades[j] - mod;
if (mod > 0) {
    round += 5;
}

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

Try this:

int num = grades[j] % 5 < 3 ? grades[j] - (grades[j] % 5) : grades[j] - (grades[j] % 5) + 5;

Here demo code:

#include <stdio.h>

int main() {
    //code
    int grades[5] = {10, 68, 12, 67, 41};
    int j;
    for (j = 0; j < 5; j++)
    {
        int num = grades[j] % 5 < 3? grades[j] - (grades[j] % 5) : grades[j] - (grades[j] % 5) + 5;
        printf("%d\n",num);
    }

    return 0;
}

Output:

10
70
10
65
40

I hope help you.

One algorithm that works is:

  1. half the round value
  2. get the remainder of number by dividing it with roundValue
  3. get quotient of division (so that we can multiply with it in end)
  4. comparing it with the half of round Value
  5. if it's greater than half it rounds to the nearest greater value
  6. if it's less than half it rounds to the nearest smaller value

In code:

     int round(int nearest , int number){
            int half = nearest / 2;
            int answer = 0;  
            int remainder = number% nearest ;
            int quotient = number/ nearest ;
            if(remainder > half ){
                answer =(quotient+1) * nearest ;
            }else{
                answer =quotient * nearest ;
            }
        return answer;
        }

I have a function to round up:

def next(number, base):
    temp = round(number / base) * base
    return temp

you can use it for your case like this:

next(grade[j], 5)

and in function it will be:

temp = round(67 / 5) * 5
return temp #temp = 75

some case not using round but using ceil

Why so much of complication. Here is the code. Have a look.

vector<int> gradingStudents(vector<int> grades) {
    int size = grades.size();
    for(int i=0;i<size;i++)
    {
        if(grades[i]>40)
        {
            if((grades[i]+2)%5==0)
            grades[i]=grades[i]+2;
            else if((grades[i]+1)%5==0)
            grades[i]=grades[i]+1;
        }
        else if(grades[i]>37&&grades[i]<40)
        grades[i]=40;
    }
return grades;
}
def next_multiple_of(number, value):
    return (value // number + 1) * 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM