So I try to devide 2 integers, that would result in a double. But the issue is, it keeps rounding it to 0 decimals. So for instance if the kills are 50 and the deaths are 20, it says that k/d is 2.0 while that is not the case. This is my code: Code:java double kd = plugin.getConfig().getInt("kills." + player.getName())/ plugin.getConfig().getInt("deaths." + player.getName());player.sendMessage(ChatColor.GREEN + "Your K/D: "+ ChatColor.RED + kd); How do I make it so that it rounds the double to 2 decimals? Example: K/D = 2.56 Thank you!
That doesn't seem to work, I tried it. Also the main issue is, that it doesn't show a double. it shows an integer with one decimal 0 For example: 1.0 Anyone know what's wrong? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
yupie_123 When you divide two integers in java it will return the outcome as int form. Try casting them to a double when you do the calculation. Try this Code:java int kills = plugin.getConfig().getInt("kills." + player.getName());int deaths = plugin.getConfig().getInt("deaths." + player.getName());double kd = (double) kills / (double) deaths;player.sendMessage(ChatColor.GREEN + "Your K/D: "+ ChatColor.RED + kd); Edit: Or if I'm not mistaken you can even use the getDouble() method to ensure the number you are getting is already a double.
hexaan Perfect! It works, but the only issue now is, how do I make it so that it rounds it to 2 decimals? I tried Derpiee 's method but it gives errors: The left-hand side of an assignment must be a variable. Thanks!
yupie_123 Derpiee Method should work fine for rounding to 2 decimals. Can you post what you are trying?