# Solved How would I do this.

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

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%

#1
2. Offline

### Technes

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

#2
3. Offline

### Twisted_Panda

Doesen't really make sense?

#3
4. Offline

### Steffion

If im right math round removes the .0

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

#5
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
`double d = 115.333; //Example         DecimalFormat numf = (DecimalFormat) DecimalFormat.getNumberInstance();        numf.setMinimumFractionDigits(1);        numf.setMaximumFractionDigits(1);        numf.setRoundingMode(RoundingMode.HALF_UP);        numf.setGroupingUsed(false);        DecimalFormatSymbols decfs = DecimalFormatSymbols.getInstance();        decfs.setDecimalSeparator('.');        numf.setDecimalFormatSymbols(decfs);         System.out.println(numf.format(d));`

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

#6
7. Offline

### viper_monster

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

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

#8
Twisted_Panda and spoljo666 like this.