# Solved KDR.....

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

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
`  double KDR2 = conf.getInt("Kills") / conf.getInt("Deaths");        conf.set("KDR", KDR2);        try {            conf.save(file);        } catch (IOException e) {            e.printStackTrace();        }        double KDR3 = conf.getDouble("KDR");p.sendMessage("Your KDR is " + KDR2)`

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

#2
3. Offline

### amhokies

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

#3
4. Offline

### ASHninja1997

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

#4
5. Offline

#5
6. Offline

### ASHninja1997

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

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

#7
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 .
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
#8
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
`double kdr = 3.142151261; //example kdrBigDecimal rounded = new BigDecimal(kdr).setScale(3, BigDecimal.ROUND_HALF_UP);player.sendMessage("Your KDR is " + rounded.toString() + ".);`

#9
10. Offline

### hapm

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.

#10