Unity seems to round small numbers to zero


I tried to calculate 1 / 6, which should be around 0.1666666, but Unity printed it as 0 (script below).

var number = 1 / 6;

I tried it with 1 / 3, 1 / 2, but it still came out zero.

When I did 10 / 2, it calculated normally and printed 5.

I’ve searched everywhere but noone seems to have the same problem…

Anyone know why? Thanks.

This question has come up a number of times on this list. 1 and 6 are integers, so you get 0 dividing them. Try 1.0 / 6.0.

What you are doing is a integer division. You are adding two ints thus the result is an int. If you want to receive a float result you have to have at least one float in youre calculation.

Debug.Log("More or less accurate: " + 1f/6);

The “f” behind the “1” indicates it is a float. You could also use “1.0”