# Why is this returning 0

## Recommended Posts

why is this returning 0? Idk if I’m just dumb or what

##### Share on other sites
3 minutes ago, skillerkidos1 said:

why is this returning 0? Idk if I’m just dumb or what

Isn't it because they're int and ints can't be decimals? So it defaults to  int prayerPercent = 100 * 0; basically?

##### Share on other sites
Posted (edited)

problem is when you're doing dynamic / static and you don't cast that value to a double that will return 0 due to rounding.

Even if it's 0.999999 it will round down to zero.

You need to cast your calculation to a double for dynamic / static and then multiply it by 100 and cast it back to an int.

Example here:

```public int getPercentage() {
double staticP = getSkills().getStatic(Skill.PRAYER);
double dynamicP = getSkills().getDynamic(Skill.PRAYER);
return (int) (100 * (dynamicP / staticP));
}```

Edited by Malcolm
• 1
• 1
##### Share on other sites
33 minutes ago, Gunman said:

Isn't it because they're int and ints can't be decimals? So it defaults to  int prayerPercent = 100 * 0; basically?

26 minutes ago, Malcolm said:

problem is when you're doing dynamic / static and you don't cast that value to a double that will return 0 due to rounding.

Even if it's 0.999999 it will round down to zero.

You need to cast your calculation to a double for dynamic / static and then multiply it by 100 and cast it back to an int.

Example here:

```
public int getPercentage() {
double staticP = getSkills().getStatic(Skill.PRAYER);
double dynamicP = getSkills().getDynamic(Skill.PRAYER);
return (int) (100 * (dynamicP / staticP));
}```

Ohhh I see now thank you very much

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.