# Solved Percentage until completion

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

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 percentage = iHave/iNeed;```
Code:
```        im.setLore(Arrays.asList(percentage));
```
Currently prints out 0.

#1
2. Online

### 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.

#2
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");

int percentage = (int) dHave;
/CODE]

However when I have 2/2 bread it shows 200%. Should I change the 100 to 50?```
Oops silly me. Forgot to change it to double.

#3
4. Online

### Zombie_Striker

@plisov
Try using this:
Code:
```int iHave = config.getInt("Data." + player.getName() + ".Tasks.Task1.Collected");

int precentag = (int)(((double)iHave)/iNeed*100);```

#4
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
#5