Solved KDR.....

Discussion in 'Plugin Development' started by ASHninja1997, Sep 27, 2013.

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

    ASHninja1997

    ok so i was just trying to add a KDR stat to my server and I Divided the Kills by Deaths and it doesn't give me the Decimals it only gives me Whole Number version. Here is my code
    Code:java
    1. double KDR2 = conf.getInt("Kills") / conf.getInt("Deaths");
    2. conf.set("KDR", KDR2);
    3. try {
    4. conf.save(file);
    5. } catch (IOException e) {
    6. e.printStackTrace();
    7. }
    8. double KDR3 = conf.getDouble("KDR");
    9. p.sendMessage("Your KDR is " + KDR2)
     
  2. Offline

    remremrem

    line 1
    declare KDR as a double instead of an int
    and change Kills and Deaths to doubles before you divide them

    actually I think if you only change Kill to double it will return a double
     
  3. Offline

    amhokies

    Changing either one of them to a double will give you a double, yes.
     
  4. Offline

    ASHninja1997

    remremrem
    Why would kills need to be a double when it is counted in "INT"ervals?
     
  5. Offline

    xTrollxDudex

  6. Offline

    ASHninja1997

    remremrem
    Yes that made it work thanks. (Working code below)
    Code:java
    1. double KDR2 = conf.getDouble("Kills") / conf.getInt("Deaths");
    2. conf.set("KDR", KDR2);
    3. try {
    4. conf.save(file);
    5. } catch (IOException e) {
    6. e.printStackTrace();
    7. }
    8. double KDR3 = conf.getDouble("KDR");
    9. p.sendMessage("Your KDR is " + KDR2);
     
  7. Offline

    amhokies


    int stands for integer. An integer is basically a number without a decimal. A double on the other hand, does allow decimal places. You cannot divide two integers together to get a double. You need to have a double and an int or both doubles.
     
  8. Offline

    ASHninja1997

    xTrollxDudex
    Just wondering.
    The KDR will come up as a long Rational Number.
    Is there a way to make it only display the decimal up to the Hundredths?

    amhokies
    Thanks for the reply but i have been coding for a bit now and I know what a int and double is along with other variables :p.
    Thanks for explaining the Last sentence that one did help.
    But again thanks for the reply

    Nvm I just changed it to float......It works better

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    Quantix

    If you want to round the KDR to three or two decimals, you can use the BigDecimal class. Here's an example of how to use it:
    Code:java
    1. double kdr = 3.142151261; //example kdr
    2. BigDecimal rounded = new BigDecimal(kdr).setScale(3, BigDecimal.ROUND_HALF_UP);
    3. player.sendMessage("Your KDR is " + rounded.toString() + ".);
     
  10. Only to give you a much more flexible version for converting a double to String, I suggest you to use the static method String.format. I'm not sure if it is faster then using a new instance of BigDecimal, but it is for sure the flexiblest way of converting a number of any standard type to a String. It enables you to convert the value into any good known representation. Look into format strings to get more details on how you can use String.format.
     
Thread Status:
Not open for further replies.

Share This Page