Answers for "why is this divide calculation returning me 0?"
http://answers.unity.com/questions/411373/why-is-this-divide-calculation-returning-me-0.html
The latest answers for the question "why is this divide calculation returning me 0?"Answer by gregroberts
http://answers.unity.com/answers/844749/view.html
This worked for me:
int A = 10;
int B = 100;
float P1 = A / B; // P1 will be 0 int
float P2 = (A * 1.0f / B); // P2 will be 0.1fMon, 01 Dec 2014 20:22:49 GMTgregrobertsAnswer by Bunny83
http://answers.unity.com/answers/411378/view.html
Because you divide with integer values so it's an integer division. An integer division always returns an integer. the fractional part is always trucated. So 1 / 2 = 0.5 -> 0
You need to use float values. When you divide by constant numbers use a float literal:
int V = 10;
float P = V / 100.0f; // P will be 0.1f
If you want to divide two integers and want a fractional part you have to cast one of the operands into a float:
int V = 10;
int D = 100;
float P1 = V / D; // P1 will be 0.0f
float P2 = (float)V / D; // P2 will be 0.1fTue, 05 Mar 2013 20:07:52 GMTBunny83