-5

I made a simple and inefficient method to round up to nearest 9. This is what i have

private int getInventorySize(int max) {
    if (max <= 9){
        return 9;
    }else if (max <= 18){
        return 18;
    }else if (max <= 27){
        return 27;
    }else if (max <= 36){
        return 36;
    }else if (max <= 45){
        return 45;
    }else if (max <= 54){
        return 54;
    }else{
        return 54;
    }

But as you can see this method isn't the best way to do this, can someone people post an example to do this in a efficient way, PS. if int max is bigger than 54.. it need to return 54 Thank you.

1
  • 1
    think about using division and modulas Oct 4, 2013 at 5:18

8 Answers 8

4

You can do:

private int getInventorySize(int max) {
    if (max <= 0) return 9;
    int quotient = (int)Math.ceil(max / 9.0);
    return quotient > 5 ? 54: quotient * 9;
}

Divide the number by 9.0. And taking the ceiling will get you the next integral quotient. If quotient is > 5, then simply return 54, else quotient * 9 will give you multiple of 9 for that quotient.

Some test cases:

// Negative number and 0
System.out.println(getInventorySize(-1));  // 9
System.out.println(getInventorySize(0));   // 9

// max <= 9
System.out.println(getInventorySize(5));   // 9
System.out.println(getInventorySize(9));   // 9

// some middle case 
System.out.println(getInventorySize(43));  // 45

// max >= 54
System.out.println(getInventorySize(54));  // 54
System.out.println(getInventorySize(55));  // 54
10
  • Actually, this isn't quite the same thing. Firstly, negative numbers don't work the same. Secondly, 9 gives you 18 in your code but 9 in the OPs code. You would need to do the -1, divide, +1 trick used for one-based modulo.
    – paxdiablo
    Oct 4, 2013 at 5:46
  • @paxdiablo I agree with the negative numbers it fails, but I don't see how 9 gives 18 here?
    – Rohit Jain
    Oct 4, 2013 at 5:49
  • Actually, my apologies, you're right. I misunderstood the code. Though I probably wouldn't use floating point for this, it does work as far as I can tell.
    – paxdiablo
    Oct 4, 2013 at 5:53
  • @paxdiablo I understand what you are saying. BTW I liked your approach. :)
    – Rohit Jain
    Oct 4, 2013 at 5:55
  • I was one of the -1ers but I've since removed it due to my wrongednessness. The other bod I can't speak for.
    – paxdiablo
    Oct 4, 2013 at 6:08
1

Look into the mod operator. It is typed % and will produce the remainder of a long division operation.

Once you have the remainder, you can greatly simplify your rounding. You know if you need to round up or down with a simple if statement. Then to find out which multiple of 9 you need to round up or down to, divide (whole number division only) with the / operator and remultiply.

1

You do a one-liner with modulo like this:

return (max>=54) ? 54 : max+(9-max%9)*Math.min(1,max%9);
6
  • @DavidWallace Thank you! Fixed it accordingly.
    – spydon
    Oct 4, 2013 at 5:39
  • OK, I've removed my downvote, because it now gives the correct answers (unless max is negative, in which case it's still wrong). But do you really consider this a "nice" solution? Oct 4, 2013 at 5:48
  • It might be a bit hard to read, but for such a simple task I find it nicer just to add a comment explaining it than having ten extra rows of code like some of the other solutions.
    – spydon
    Oct 4, 2013 at 5:53
  • You didn't consider the equivalent max >= 54 ? 54 : (max + 8) / 9 * 9 at all? Which, by the way is still wrong for negatives and zero. Oct 4, 2013 at 5:56
  • 1
    Or even max >= 54 ? 54 : max + (54 - max) % 9, if you must use the % operator, which is also equivalent to your solution? I really should stop burying correct solutions in comments, where the OP probably isn't going to read them. (Disclaimer - I am aware that both of the solutions I've given are incorrect for zero and negatives, but neither is difficult to adjust for these cases) Oct 4, 2013 at 6:03
0
private int getInventorySize(int max) {
    if (max < 9) {
        return 9;
    }
    if (max <= 54 && max > 9) {
        int a = max / 9;
        return a * 9;
    } else
        return 54;
}
0
private int getInventorySize (int max) {
    if (max < 9) return 9;
    for (int i = 9; i <= 54; i += 9) {
        if (max <= i) {
            return i;
        }
    }
    return 54;
}
3
  • I think you mean i+=9 Oct 4, 2013 at 5:22
  • You may also want to return 54 at the end (for example, if the input is 99). But I'm not going to downvote anyone here after I've been suitably (and quite rightly) called by @Rohit for my wrongosity :-)
    – paxdiablo
    Oct 4, 2013 at 6:12
  • Never mind, I made the requisite changes to get it to work beyond the normal bounds.
    – paxdiablo
    Oct 4, 2013 at 7:00
0

The easiest way to do it with a formula is probably with integer divide, catching the special conditions first:

private int getInventorySize (int max) {
    if (max <  1) return  9;
    if (max > 54) return 54;
    max += 8;
    return max - (max % 9);
}

The following table shows how this works:

max   max+8[A]   A%9[B]   A-B
---   --------   ------   ---
 <1                         9
  1          9        0     9
  2         10        1     9
  :
  8         16        7     9
  9         17        8     9
 10         18        0    18
  :
 18         26        8    18
 19         27        0    27
  :
 53         61        7    54
 54         62        8    54
>54                        54

However, keep in mind that there's nothing intrinsically wrong with what you've proposed in your question, but you can clean it up considerably, to the point where it's more understandable than the math-based solutions (if your input range was larger, you wouldn't use this method since the number of if statements would become unwieldy):

private int getInventorySize (int max) {
    if (max <=  9) return  9;
    if (max <= 18) return 18;
    if (max <= 27) return 27;
    if (max <= 36) return 36;
    if (max <= 45) return 45;
    return 54;
}

The use of the if ... return ... else construct is totally unnecessary since the else is superfluous - if it wasn't going to happen the return would have already returned.

Similarly, there's no point having two separate cases returning 54 when they can be combined into one.

Honestly, if you're not expecting the input range to increase, I'd actually go for the second solution since, given its size, it's actually more easily understood.

As you can see from the following test harness, both these methods work (change which one is commented out to switch between them). I would suggest plugging any of the other answers here into the test harness to ensure they also work (you'll have to make them similarly static to get them to work as is):

public class Tester {
    private static int getInventorySize (int max) {
        if (max < 1) return 9;
        if (max > 45) return 54;
        max += 8;
        return max - (max % 9);
    }

    //private static int getInventorySize (int max) {
    //    if (max <=  9) return  9;
    //    if (max <= 18) return 18;
    //    if (max <= 27) return 27;
    //    if (max <= 36) return 36;
    //    if (max <= 45) return 45;
    //    return 54;
    //}

    public static void check (int a, int b) {
        int sz = getInventorySize(a); 
        if (sz != b)
            System.out.println ("Error, " + a + " -> " + sz + ", not " + b);
    }

    public static void main (String [] args) {
        for (int i = -9999; i <= 9; i++) check (i, 9);
        for (int i = 10; i <= 54; i += 9) {
            check (i+0, i+8); check (i+1, i+8); check (i+2, i+8);
            check (i+3, i+8); check (i+4, i+8); check (i+5, i+8);
            check (i+6, i+8); check (i+7, i+8); check (i+8, i+8);
        }
        for (int i = 55; i <= 9999; i++) check (i, 54);
    }
}

And, just as an aside, I'm not entirely certain that it's kosher to round up your inventory like that (assuming the function name is accurate). I can imagine cases where you may want to round down your inventory (such as wanting to keep some stock in reserve) but rounding up seems like a big fat lie.

What are you going to tell your customers who paid good money for stock when they come in to find said stock is actually depleted?

Or are you one of those shonky operators who will do anything to get customers into the store? :-)

0
0
int round(int num) {
        return (num>54)?54:(num%9==0)?num:((num/9)+1)*9;
}
0

its super easy! here you go:

Math.ceil((number/9))*9
1
  • 1
    Your solution ignores the case when number is bigger than 54.
    – Artjom B.
    Nov 5, 2015 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.