Rounding up a number to nearest multiple of 5

后端 未结 21 1906
感动是毒
感动是毒 2020-11-27 15:24

Does anyone know how to round up a number to its nearest multiple of 5? I found an algorithm to round it to the nearest multiple of 10 but I can\'t find this one.

T

相关标签:
21条回答
  • 2020-11-27 16:12

    Recursive:

    public static int round(int n){
        return (n%5==0) ? n : round(++n);
    }
    
    0 讨论(0)
  • 2020-11-27 16:14
    int roundUp(int n) {
        return (n + 4) / 5 * 5;
    }
    

    Note - YankeeWhiskey's answer is rounding to the closest multiple, this is rounding up. Needs a modification if you need it to work for negative numbers. Note that integer division followed by integer multiplication of the same number is the way to round down.

    0 讨论(0)
  • 2020-11-27 16:14
    int roundUp(int num) {
        return (int) (Math.ceil(num / 5d) * 5);
    }
    
    0 讨论(0)
  • 2020-11-27 16:17

    Here's what I use for rounding to multiples of a number:

    private int roundToMultipleOf(int current, int multipleOf, Direction direction){
        if (current % multipleOf == 0){
            return ((current / multipleOf) + (direction == Direction.UP ? 1 : -1)) * multipleOf;
        }
        return (direction == Direction.UP ? (int) Math.ceil((double) current / multipleOf) : (direction == Direction.DOWN ? (int) Math.floor((double) current / multipleOf) : current)) * multipleOf;
    }
    

    The variable current is the number you're rounding, multipleOf is whatever you're wanting a multiple of (i.e. round to nearest 20, nearest 10, etc), and direction is an enum I made to either round up or down.

    Good luck!

    0 讨论(0)
  • 2020-11-27 16:19

    I've created a method that can convert a number to the nearest that will be passed in, maybe it will help to someone, because i saw a lot of ways here and it did not worked for me but this one did:

    /**
     * The method is rounding a number per the number and the nearest that will be passed in.
     * If the nearest is 5 - (63->65) | 10 - (124->120).
     * @param num - The number to round
     * @param nearest - The nearest number to round to (If the nearest is 5 -> (0 - 2.49 will round down) || (2.5-4.99 will round up))
     * @return Double - The rounded number
     */
    private Double round (double num, int nearest) {
        if (num % nearest >= nearest / 2) {
            num = num + ((num % nearest - nearest) * -1);
        } else if (num % nearest < nearest / 2) {
            num = num - (num % nearest);
        }
        return num;
    }
    
    0 讨论(0)
  • 2020-11-27 16:20

    Some people are saying something like

    int n = [some number]
    int rounded = (n + 5) / 5 * 5;
    

    This will round, say, 5 to 10, as well as 6, 7, 8, and 9 (all to 10). You don't want 5 to round to 10 though. When dealing with just integers, you want to instead add 4 to n instead of 5. So take that code and replace the 5 with a 4:

    int n = [some number]
    int rounded = (n + 4) / 5 * 5;
    

    Of course, when dealing with doubles, just put something like 4.99999, or if you want to account for all cases (if you might be dealing with even more precise doubles), add a condition statement:

    int n = [some number]
    int rounded = n % 5 == 0 ? n : (n + 4) / 5 * 5;
    
    0 讨论(0)
提交回复
热议问题