using UnityEngine;
using System.Collections;
public class Math : MonoBehaviour {
int value = 10;
float ratio1 = 1.1f;
float ratio2 = 1.2f;
float ratio3 = 1.3f;
void Start () {
Debug.Log (value * ratio1);
Debug.Log (value * ratio2);
Debug.Log (value * ratio3);
Debug.Log ((int)(value * ratio1));
Debug.Log ((int)(value * ratio2));
Debug.Log ((int)(value * ratio3));
}
}
I expect the result will be:
11
12
13
11
12
13
However, the actual result is:
11
12
13
11
12
12
How can I fix the problem?
Use [Mathf.RoundToInt()][1]: Debug.Log(Mathf.RoundToInt(value * ratio1)); [1]: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.RoundToInt.html
– robertbuBy way of explanation, typecasting to
– Dave-Carlileintjust truncates off the decimals. If you want to round to the nearestintyou need to use a rounding function.