Autosize to multiples of nine (for iconmenu or something else)

Discussion in 'Resources' started by hawkfalcon, Aug 14, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    hawkfalcon

    I was in need of having my icon menu auto size itself to multiples of 9, and stumbled upon a fairly simple and powerful way to do this. Here it is:
    Code:
    public int autoSize(int original) {
    return (original >= 0 ? ((original + 8) / 9) * 9 : (original / 9) * 9);
    }
    This will give you a multiple of 9 based on the number you feed it.
    Enjoy :)
     
  2. Offline

    Jogy34

    I had to do a pretty much the same thing for my own inventory menus for my own plugin. This is how I did it though:
    Code:java
    1.  
    2. protected static int toNineDenom(int from)
    3. {
    4. return ((((from / 9) + ((from % 9 == 0) ? 0 : 1)) * 9));
    5. }
    6.  


    I don't understand in your method why you check if the original value is greater than or equal to zero and then if it isn't why you divide it by 9 and then multiply it by 9 when that will always just give you 0 or a negative number.
     
  3. Offline

    Kazzababe

    Code:
    (int) Math.ceil(size / 9) * 9;
    Code:
    (int) Math.max(9, Math.round(size / 9) * 9);
    Using a Math method.
     
    bobacadodl and chasechocolate like this.
  4. Offline

    chasechocolate

    Kazzababe yeah, that what I've been using.
     
  5. Offline

    Tzeentchful

    Kazzababe chasechocolate
    The Math method might cause a little extra overhead. I may be wrong though. A micro benchmark would be interesting. Not that it really matters considering how often the code would be run.
     
    hawkfalcon likes this.
  6. Offline

    TutorialMakerHD

    Code:
    public int autoSize(int original) {
        while(original % 9 != 0)
            original++;
        return original;
    }
    I know this method is not better! But i think it's easier to understand :)
     
  7. Offline

    Tirelessly

    Since everybody else is doing it...
    Code:java
    1.  
    2. private static int toNine(int notNine){
    3. return notNine + (9 - (notNine % 9));
    4. }
    5.  

    Doesn't work for negative numbers
     
Thread Status:
Not open for further replies.

Share This Page