Trying to store a decimal between 0&1 after dividing by 100

• 10-28-2010, 11:31 AM
jebbo
Trying to store a decimal between 0&1 after dividing by 100
Hi, I'm not totally new to Java, been playing for a while but I've hit an embarassing brick wall. The answer to this must be simple but I just can't get it!!!

Basically I'm playing with making a little text based game. Each enemy has an amount of damage it can do, and I'm trying to make it relate to it's health.

ie a damage of 10 and 100 health returns 10
but a damage of 10 and 50 health returns 5.

I've tried to divide the health by 100 to get a decimal value then multiply by the damage to lower it sufficiently but for some reason Java stores a value of 0 when I do the divide by 100. Here's some code i quickly tested to check I wasn't going mad (the real game has loads of methods that would need explaining to get the values):

System.out.println(groups.jake.getHealth()); //correctly outputs value 50
double healthTest = groups.jake.getHealth()/100; //should store 0.5
System.out.println(healthTest); //outputs 0.0 instead of 0.5

What's going on?
• 10-28-2010, 12:16 PM
DarrylBurke
Integer arithmetic. Either have getHealth() return double, use a denominator that is a double literal or cast either the numerator or denominator to double to indicate to the compiler that floating point arithmetic is to be done.
Code:

`double healthTest = groups.jake.getHealth()/100.0;`
db

db
• 10-28-2010, 01:56 PM
jebbo
Haha how simple, just make the 100 a double, probably would've stumbled across that thought pattern sooner or later - I can feel the egg running down my face!

Thanks, sure I'll be posting more soon