Solved Calculation giving wrong answer

Discussion in 'Plugin Development' started by CheesyFreezy, Jun 10, 2016.

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


    Heey guys,

    I am back at it again with a really small problem. I am getting a percentage of a progress from a money balance. So I made a calculation where it should give us the percentage of the money that the player has compared to the money that the player needs to do stuff. But the answer of this calculation is 0.0 for some reason. I checked the calculation in a calculator and the answer was right, but not in the plugin.
    Please help me!

    DecimalFormat format = new DecimalFormat("#.0");
    double calculation = 100 / getConfig().getInt("prison-ranks." + rank) * moneyConfig.getInt(players.getUniqueId().toString());
    String percentage = format.format(calculation).replace(",", ".");
  2. Offline


    For example, the player has 67$. He needs 225$.

    1. Calculate the percentage: playermoney / neededmoney, which gives 0,2977777777777778 in this example
    2. Multiply it by 100 and then apply the decimalformat and you have: 29.8%
    System.out.println the values from the config to make sure you got them right
  3. Offline


    Try replacing 100 with 100.0D in your second line. Otherwise it might cast your calculation result to int before casting it to double, because all the other factors are int as well.
  4. Offline


Thread Status:
Not open for further replies.

Share This Page