[C#] An extremely bizarre problem.

UndeadDragon

Super Moderator
Reaction score
447
I have turned to the members of here to try and figure out what the problem is with this, because I give up.

Why can my program not do ((80/100)*100)*5.

Simple enough formula. It works out the percentage of health that an enemy has left and then *5 to add it to the health bar. Why does this return 0?!

No matter what I try I cannot make it return anything but 0. It will be using the variables, so it will be in this form:

int healthBarWidth = ((enemyHealth / maxEnemyHealth) * 100) * 5;

However, it also returns 0 when I use absolute values, 80 and 100 for example. You can try it in a calculator, it returns 400.

Here's my code if anyone can shed some light on it:

Code:
Ability ability = new Ability(abilityPressed);
int damage = calculateDamage(abilityPressed);
enemyHealth -= damage;
MessageBox.Show(maxEnemyHealth.ToString() + "  " + enemyHealth.ToString());
int healthBarWidth = ((enemyHealth / maxEnemyHealth) * 100) * 5;
MessageBox.Show(healthBarWidth.ToString());
healthBar.Width = healthBarWidth;
MessageBox.Show("(("+enemyHealth.ToString() + "/" + maxEnemyHealth.ToString() + ") * 100) * 5");

The message boxes that appear are: "80 100", "0" and "((80/100)*100)*5"

Thanks.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Code:
int i = ((80 / 100) * 100) * 5;
float f = (((float)80 / (float)100) * (float)100) * (float)5;
Console.WriteLine(i); // displays 0
Console.WriteLine(f); // displays 400
Console.ReadKey();
typecast ftw.

damn blade's too fast!
 

UndeadDragon

Super Moderator
Reaction score
447
Ah thanks. I completely forgot about the fact that the decimal would be dropped >.< That's what I get for programming when I'm tired.

What a fool :p

+rep to both of you.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top