Anomaly in getting a percentage

myGameObject.GetComponent().text = ((100 / 1000) * 100).ToString();

Above is a line of code that I have been working with. I expect there is a problem with the calculation I’m making because the output I receive is 0. However, when I replace the calculation with any other text I do get an output.

What could be preventing this calculation from working?

Rounding. It’s treating the numbers as integers so the decimal point produced by the 100/1000 is rounded to 0 before it’s multiplied by 100. stick an f or 2 in there to let it know you want it treated as floats instead.(100f / 1000f) * 100

1 Like

Perfect. Thank you!

Actually i should correct myself here. It’s not rounding the number, it’s simply cutting the decimal off. Even if the result is 0.9 it will still come out 0. Just for the sake of being properly correct.

1 Like