个人技术分享

如果你想在 Unity 中保留浮点数的一位小数,你可以使用 ToString() 方法并指定格式化字符串,或者使用 Mathf.Round() 方法来四舍五入到一位小数。以下是两种方法的示例:

使用 ToString() 方法:

float floatValue = 3.456f; 

floatValue.ToString("F1"); 

使用 Mathf.Round() 方法:

float floatValue = 3.456f; // 你的浮点数

float roundedValue = Mathf.Round(floatValue * 10f) / 10f; // 四舍五入到一位小数 Debug.Log(roundedValue); // 输出:3.5

使用 Mathf.Round() 方法2:

Mathf.Round(floatValue ,1) ;