【Unity】四舍五入算法


        /// 
        /// 四舍五入
        /// 
        /// digits:保留几位小数
        public static float Round(this float value, int digits = 1)
        {
            float multiple = Mathf.Pow(10, digits);
            float tempValue = value * multiple + 0.5f;
            tempValue = Mathf.FloorToInt(tempValue);
            float finalValue = tempValue / multiple;
            return finalValue;
        }

        /// 
        /// 四舍五入到整数
        /// 
        /// 
        /// 
        public static int RoundToInt(this float value)
        {
            float tempValue = value.Round(0);
            int finalValue = Mathf.FloorToInt(tempValue);
            return finalValue;
        }

你可能感兴趣的:(【Unity】四舍五入算法)