Solved Percentage until completion

Discussion in 'Plugin Development' started by plisov, Mar 20, 2017.

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

    plisov

    I'm trying to have a percentage be created from two numbers. Lets say we have two variables, need and have. Need is the amount of lets say bread you need to complete an achievement. Have is how much you have currently. Lets say you have 8 bread and you need 10 bread to complete the achievement. I would like to make the percentage print out as 80%

    I currently have
    Code:
    int iHave = config.getInt("Data." + player.getName() + ".Tasks.Task1.Collected");
            int iNeed = config.getInt("Data." + player.getName() + ".Tasks.Task1.Need");
    
            int percentage = iHave/iNeed;
    Code:
            im.setLore(Arrays.asList(percentage));
    
    Currently prints out 0.
     
  2. Offline

    Zombie_Striker

    @plisov
    First, make sure you case iHave to a double. If you want the value to be a decimal (like 0.45 ), keep it the way it is. If you want full numbers (45%), multiply the double by 100 and that number back to an int.
     
  3. Offline

    plisov

    Ah. Alright. So I've changed it to look like this
    Code:
            double dHave = config.getInt("Data." + player.getName() + ".Tasks.Task1.Collected");
            double dNeed = config.getInt("Data." + player.getName() + ".Tasks.Task1.Need");
    
            int iHave = config.getInt("Data." + player.getName() + ".Tasks.Task1.Collected");
            int iNeed = config.getInt("Data." + player.getName() + ".Tasks.Task1.Need");
    
            int percentage = (int) dHave;
    /CODE]
    
    However when I have 2/2 bread it shows 200%. Should I change the 100 to 50?
    ADDED:
    Oops silly me. Forgot to change it to double.

    ADDED: Still shows 200
     
  4. Offline

    Zombie_Striker

    @plisov
    Try using this:
    Code:
    int iHave = config.getInt("Data." + player.getName() + ".Tasks.Task1.Collected");
    int iNeed = config.getInt("Data." + player.getName() + ".Tasks.Task1.Need");
    
    int precentag = (int)(((double)iHave)/iNeed*100);
     
  5. Offline

    plisov

    Ah. I see what you mean. How is this working? It's setting iHave as a double and dividing it by int iNeed times 100 and setting the end number as an integer?
    Thanks @Zombie_Striker
    Always can count on you! :)
     
    Last edited: Mar 20, 2017
Thread Status:
Not open for further replies.

Share This Page