Solved Double - Limit amount of digits after decimal?

Discussion in 'Plugin Development' started by elementalgodz11, Jan 18, 2014.

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

    elementalgodz11

    When a player kills someone, I add 1 integer point to their kills, vice versa with deaths.

    I tried to devide the kills by deaths to get the players KD (Kills/Deaths) ratio.

    It works, but if the kills doesn't devide well with the deaths, many decimal numbers may be used.

    Here is an example:
    Screenshot_1.png

    How could I only make two decimal values appear removing the unnecessary digits? For example, instead of 21.35957392729, something like 21.36, rounding the value over.

    Here is the code I have used:
    Code:java
    1. FileConfiguration kills = KitsMain.statskills.getStatsKillsData();
    2. FileConfiguration deaths = KitsMain.statsdeaths.getStatsDeathsData();
    3. double kd = kills.getDouble(target.getName()) / deaths.getDouble(target.getName());
    4. p.sendMessage(ChatColor.RED + "KD: " + ChatColor.WHITE + kd);


    Thanks.
     
  2. Offline

    Henzz

  3. Offline

    werter318

    Henzz He said he wanted 2 decimal numbers.
     
    Henzz and elementalgodz11 like this.
  4. Offline

    Mattigins

    Code:java
    1. doubleRoundTo2Decimals(double val){
    2. DecimalFormat df2 =newDecimalFormat("###.##");
    3. return Double.valueOf(df2.format(val));
    4. }
     
    elementalgodz11 likes this.
  5. Offline

    elementalgodz11

  6. Offline

    werter318

    elementalgodz11 and to make sure it works on all server you should do df2.setLocale(Locale.US), otherwise you are going to get 4,5 for example (I ran into that once).
     
  7. Offline

    elementalgodz11

    @weter318 I don't mind if it rounds it to 1 decimal place, just a maximum of 2.
     
  8. Offline

    werter318

    elementalgodz11 That's not what I meant :D, I meant the comma, Double.valueOf() can't parse ,'s
     
  9. Offline

    Mattigins

    I believe in europe they use commas as decimals.
     
  10. Offline

    werter318

  11. Offline

    elementalgodz11

    @
    werter318
    Okay but there is no setLocal or Locale method, did you mean setCurrency?
     
Thread Status:
Not open for further replies.

Share This Page