The code here is straight forward but I don't understand the results:
float percent = 0.69f;
int firstInt = (int)(percent*100f);
float tempFloat = percent*100f;
int secondInt = (int)tempFloat;
Debug.Log(firstInt + " " + secondInt);
Why is firstInt
68 but secondInt
is 69?
69 69
.. – Peckham68 69
result. – Eserine69
can't be accurately represented by a float and actually comes out to something like 68.999999... Changing your second line of code toint firstInt = (int)Math.Ceiling(percent*100f);
gives you 69. No idea why assigning that 68.99999... to a float variable "corrects" the problem though. – Sold68 68
with optimizations enabled, and68 69
without. Weird. – Peckham