Solved Numbers after decimal

Discussion in 'Plugin Development' started by Funergy, May 1, 2014.

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

    Funergy

    Hey guys,
    I already found a answer on google but it didn't help me alot
    so what I wanna do is my K/D is now 1.2353534258334...
    but I wanna set it to 1.235
    But I don't know how to do that
     
  2. Offline

    itzrobotix

    double number = 1.235; Like dat?
     
  3. Offline

    Funergy

    yes normally is the k/d longer but it needs to be 3 numbers after the decimal
     
  4. Offline

    itzrobotix

    So do you already have a predefined number for the kills and death if so you could just divide them?
     
  5. Offline

    Funergy

    this is what i have
    Code:
    if(args[0].equalsIgnoreCase("stats")){
                    Player p = (Player) sender;
                    p.sendMessage("§bCubox§f» §cKills: " + getkills(p.getName()));
                    p.sendMessage("§bCubox§f» §cDeaths: " + getdeaths(p.getName()));
                    p.sendMessage("§bCubox§f» §cK/D: " +(double) getkills(p.getName())/getdeaths(p.getName()))
     
  6. Offline

    DxDy

  7. Offline

    Shadowwolf97

    Also, you can round the number using simple math in Java. To round out by 3 decimals, this should do it.
    Code:java
    1. double kd = Math.round(someDouble * 1000) / 1000;

    This will move the decimal 3 places, round off that last decimal, and then move the decimal point back 3 places making it a decimal again.
     
  8. Offline

    BillyGalbreath

    Use DecimalFormat.

    Example:

    Code:java
    1.  
    2. double value = 1.2353534258334;
    3.  
    4. DecimalFormat df = new DecimalFormat("#.000");
    5.  
    6. String output = df.format(value);
    7.  
     
  9. Offline

    DxDy

    Line 4 should read: DecimalFormat df = new DecimalFormat("#.000");
     
    BillyGalbreath likes this.
  10. Offline

    ZodiacTheories

  11. Offline

    Funergy

    I want 3 numbers after the decimal
    but it says 1.000
     
  12. Offline

    BillyGalbreath

    You got 3 digits behind the decimal. Thats exactly what you wanted.
     
    DoctorDark, unforgiven5232 and Pew446 like this.
  13. Offline

    kennethbgoodin

    Do
    DecimalFormat df = new DecimalFormat("#.###");
     
    unforgiven5232 likes this.
  14. Offline

    Funergy

    When I do /ffa stats then
    it says K/D: 1
    but without decimals
     
  15. Offline

    t7seven7t

    Surprised that no one has mentioned String.format yet.

    Funergy try this:
    Code:java
    1. String.format("%.3f", kdr);
     
  16. Offline

    Funergy

    It just says 1.000 and not 1.245 or something like that
    like it says 2.000 but not 2.355
     
  17. Offline

    t7seven7t

    Funergy Then something is wrong with your division. Make sure you are doing double or float division to get your kdr and not integer division. You can cast kills and deaths to float/doubles first.
     
  18. Offline

    Funergy

    this is what I have
    Code:
    double kdfirst = getkills(p.getName())/getdeaths(p.getName());
                    String kd = String.format("%.3f", kdfirst);
                    p.sendMessage("§bCubox§f» §cK/D: " +kd);
     
  19. Offline

    DxDy

    Cast them to double to avoid integer division.
     
  20. Offline

    yupie_123

    Funergy I have a similar thing in my code, I want K/D to have 2 decimals so I used this:
    Code:java
    1. kd = Math.round(kd * 100.0) / 100.0;
    2.  


    I suppose if you would like 3 decimals you have to use:

    Code:java
    1. kd = Math.round(kd * 1000.0) / 1000.0;
    2.  


    I will test this myself in a bit!

    EDIT: I tested it and it works perfectly! You should also cast them to double as DxDy said.
     
  21. Offline

    Funergy

    Doesn't work it shows me 1.0 and not 1.235
    Code:
    double kdfirst = getkills(p.getName())/getdeaths(p.getName());
                    double kd = Math.round(kdfirst * 1000.000) / 1000.000;
                    p.sendMessage("§bCubox§f» §cK/D: " +(double)kd);
     
  22. Offline

    DxDy

    As we told you.
    Code:java
    1. double kdfirst = (double)getkills(p.getName())/(double)getdeaths(p.getName());

    Afterwards any method from this thread will work.
     
  23. Offline

    Funergy

    Thanks it worked
    setting thread SOLVED
     
  24. Offline

    BillyGalbreath

    For future reference, this will only print 3 digits after the decimal if any only if a value is there. Use the example I posted earlier ("#.000") to force the three digits even if no value is there.
     
Thread Status:
Not open for further replies.

Share This Page