Comments and 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 comments and answers for the question "why is this divide calculation returning me 0?"Comment by Westland on Westland's answer
http://answers.unity.com/comments/1556336/view.html
In case someone else misses this (like I did) - If you're doing:
image.fillAmount = (intA / intB);
To make it work it needs to be:
image.fillAmount = ((float)intA / intB);
Might be other versions, still learningMon, 24 Sep 2018 23:18:41 GMTWestlandComment by Pangamini on Pangamini's answer
http://answers.unity.com/comments/1074130/view.html
you can just use the cast
float P2 = ((float)A / B)Wed, 30 Sep 2015 09:40:30 GMTPangaminiAnswer 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 GMTgregrobertsComment by sdgd on sdgd's answer
http://answers.unity.com/comments/411379/view.html
wow didn't know that thanks now I know why somewhere else I got some problems but it somehow worked but not as I wantedTue, 05 Mar 2013 20:12:09 GMTsdgdAnswer 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