Unity Mathf/Math 数学运算函数

一、Mathf.Round  四舍五入

 四舍五入取最接近的整数,返回值是 float 类型

 如果数字的末尾是 .5,不管是偶数还是奇数,将返回偶数

例如:

1    Debug.Log(Mathf.Round(10.0f));
2    Debug.Log(Mathf.Round(10.2f));
3    Debug.Log(Mathf.Round(10.7f));
4    Debug.Log(Mathf.Round(10.5f));
5    Debug.Log(Mathf.Round(11.5f));

 由于末尾是 0.5,所以返回偶数

 而 11 不是偶数,所以分别返回 10 和12

二、Mathf.RoundToInt  四舍五入

 四舍五入取最接近的整数,返回值是 Int 类型整数

 如果数字末尾是 .5,不管是偶数还是奇数,将返回偶数

例如:

1    Debug.Log(Mathf.RoundToInt(10.0f));
2    Debug.Log(Mathf.RoundToInt(10.2f));
3    Debug.Log(Mathf.RoundToInt(10.7f));
4    Debug.Log(Mathf.RoundToInt(10.5f));
5    Debug.Log(Mathf.RoundToInt(11.5f));

 由于末尾是 0.5,所以返回偶数

 而 11 不是偶数,所以分别返回 10 和12

三、Mathf.Ceil  向上限值取整

 向上限值取整,返回值是 float 类型

 如果是负数 -8.8,由于取上原则,则取 -8

例如:

1    Debug.Log(Mathf.Ceil(8)); 
2    Debug.Log(Mathf.Ceil(8.1f));
3    Debug.Log(Mathf.Ceil(8.5f));
4    Debug.Log(Mathf.Ceil(8.8f));
5    Debug.Log(Mathf.Ceil(-8.5f));
6    Debug.Log(Mathf.Ceil(-8.9f));

四、Mathf.Floor  向下限值取整

 向下限值取整,返回值是 float 类型

 如果是负数 -8.8,由于取下原则,则取 -9

例如:

1    Debug.Log(Mathf.Floor(8));
2    Debug.Log(Mathf.Floor(8.1f)); 
3    Debug.Log(Mathf.Floor(8.5f)); 
4    Debug.Log(Mathf.Floor(8.8f)); 
5    Debug.Log(Mathf.Floor(-8.5f)); 
6    Debug.Log(Mathf.Floor(-8.9f));

原文:https://blog.csdn.net/ChinarCSDN/article/details/81284727

*** |  以上内容仅为学习参考、学习笔记使用  | ***

原文地址:https://www.cnblogs.com/ChenZiRong1999/p/13383705.html