- Home /
Major int interacting with lesser float
So, I want a int to change a float value, but the int is bigger than the float value, just an example of what I have:
public int curArmor = 100; public float armorFillBar = 1.0f;
The float is used to determinate how much the filler bar of the armor counter is visible, I can't interact with the filler itself from the int, so I've to pass the value first to the float. How can I calculate the int to the float? Like if curArmor goes to 90, the float would be 0.9 and so on.
I can make it with if statements, but it would be just a dirty workaround.
Answer by lpye · Sep 11, 2012 at 05:17 PM
Presumably you also have an int that defines your maxArmor.
There are other ways to get what you want, but the following should work:
armorFillBar = (float)curArmor / (float)maxArmor;
Optionally, if you know you will never need to use maxArmor in an int context, you could just make it a float to begin with, removing the need for the cast on maxArmor.
armorFillBar = (float)curArmor / maxArmor;
Yes I got also a maxArmor, but I have to cast it too as it is referenced for other stuff and would make things more complicated beign a float, but I'll keep it in $$anonymous$$d for the future.
Thanks for your help, everything works good, I feel a little embarassed for not co$$anonymous$$g up with that solution :D.
Your answer
Follow this Question
Related Questions
Can I create a list with an int/float and a string? C# 2 Answers
Have a problems with a values 1 Answer
Convert a char to int / float 2 Answers
float = int / int float value always 0 1 Answer
Multiple Cars not working 1 Answer