The problem when convert float to int

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?

This is likely due to the fact that floats have limited precision. Under the hood, 10 * 1.3 may be 12.99999962 or something similar.

When you do a Debug.Log, what you’re really saying is Debug.Log((my message).ToString());

So you are actually doing two things here:

Top 3: multiplying an int by a float produces a float, so you have float.ToString()

Bottom 3: you cast to int first here, so you have int.ToString()

When you cast from float to int, it throws away all the fractional part. So 12.99999962 becomes 12, which is then converted to a string as 12. float.ToString() is smart enough to round your number, rather than truncating it.