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

1. Member
Join Date
Oct 2010
Posts
2
Rep Power
0

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

2. 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.
Java Code:
`double healthTest = groups.jake.getHealth()/100.0;`
db

db

3. Member
Join Date
Oct 2010
Posts
2
Rep Power
0
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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•