Solved How would I do this.

Discussion in 'Plugin Development' started by Twisted_Panda, Oct 16, 2013.

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

    Twisted_Panda

    How would I get 100.0 on a message.
    Code:
     ChatColor.GRAY +  " - Health: " + percent + "%"
    I used
    Code:
      long percent = Math.round(((float) p.getHealth() / (float) p.getMaxHealth()) 
    and got 100%
     
  2. Offline

    Technes

    p.getHealth() and p.getMaxHealth() both return Doubles, so if you don't cast float, it should return what you desire.
     
  3. Offline

    Twisted_Panda

    Doesen't really make sense?
     
  4. Offline

    Steffion

    If im right math round removes the .0
     
  5. Offline

    drtshock

    Twisted_Panda Why are you casting to float?

    Percentage is gotten by dividing one number by another, then multiplying by 100. Then you can just put that right in your string.
     
  6. Offline

    whitehooder

    I guess what you mean by your question is to have decimals at all times, also to limit to one decimal.
    I would recommend you to use DecimalFormat for this.

    You could do it this way (long):
    Code:java
    1. double d = 115.333; //Example
    2.  
    3. DecimalFormat numf = (DecimalFormat) DecimalFormat.getNumberInstance();
    4. numf.setMinimumFractionDigits(1);
    5. numf.setMaximumFractionDigits(1);
    6. numf.setRoundingMode(RoundingMode.HALF_UP);
    7. numf.setGroupingUsed(false);
    8. decfs.setDecimalSeparator('.');
    9. numf.setDecimalFormatSymbols(decfs);
    10.  
    11. System.out.println(numf.format(d));


    or like this:
    Code:java
    1. double d = 115.333; //Example
    2.  
    3. DecimalFormat decf = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
    4. decf.applyPattern("#0.0"); // Info: "#,#00.0#" -> 1,234.56
    5.  
    6. System.out.println(decf.format(d));
     
  7. Offline

    viper_monster

    whitehooder, couldn't he get the percentage of a players health by doing this: (p.getHealth / p.getMaxHealth ) * 100 ?
     
  8. Offline

    whitehooder

    spoljo666
    Sure, that is how he would get the percentage. He could then store that percentage in the double d value and format it the way he wanted with DecimalFormat. This is useful when you have a number like 100/3=33.3333333333333 and don't want to output all the three's
     
    Twisted_Panda and spoljo666 like this.
Thread Status:
Not open for further replies.

Share This Page