- Home /
Duplicate Question
Unity seems to round small numbers to zero
Hi.
I tried to calculate 1 / 6, which should be around 0.1666666, but Unity printed it as 0 (script below).
var number = 1 / 6;
print(number);
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.
Thanks! But I have to use a decimal number on every calculation? So it looks like I have a whole script to fix...
Either use 1.0 / 6.0 as suggested, or as I prefer, just be clear that you want the numbers to be handled as floats: 1f / 6
Answer by robertbu · May 12, 2013 at 04:36 PM
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.
Answer by Mexallon · May 12, 2013 at 05:04 PM
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"