c# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数相关的知识,希望对你有一定的参考价值。

/// <summary>
        /// 实现数据的四舍五入法
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>四舍五入后的结果</returns>
        public decimal Round(decimal v, int x)
        {
            var isNegative = false;
            //如果是负数
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            var IValue = 1;
            for (var i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }

            var Int = Math.Round(v * IValue, 0);
            v = Int / IValue;

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }

        /// <summary>
        /// 实现数据的向上取整
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>向上取整后的结果</returns>
        public decimal Ceiling(decimal v, int x)
        {
            var isNegative = false;
            //如果是负数
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            var IValue = 1;
            for (var i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }

            var Int = Math.Ceiling(v * IValue);
            v = Int / IValue;

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }

        /// <summary>
        /// 实现数据的向下取整
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>向下取整后的结果</returns>
        public decimal Floor(decimal v, int x)
        {
            var isNegative = false;
            //如果是负数
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            var IValue = 1;
            for (var i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }

            var Int = Math.Floor(v * IValue);
            v = Int / IValue;

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }

        /// <summary>
        /// 实现见角进元
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>见角进元后的结果</returns>
        public decimal SeeDownUp(decimal v, int x)
        {
            var isNegative = false;
            //如果是负数
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            var IValue = 1;
            for (var i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }

            var stirngpanduan = Math.Floor(v * IValue * 10).ToString();
            if (stirngpanduan.Substring(stirngpanduan.Length - 1) == "0")
            {
                var Int = Math.Floor(v * IValue);
                v = Int / IValue;
            }
            else
            {
                var Int = Math.Ceiling(v * IValue);
                v = Int / IValue;
            }

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }

        /// <summary>
        /// 实现数据的四舍五入,向上取整,向下取整
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <param name="type">1四舍五入,2向上取整,3向下取整</param>
        /// <returns>decimal</returns>
        public decimal DecimalProcessing(decimal v, int x, string type)
        {
            decimal result = 0;
            switch (type)
            {
                case "1":
                    result = Round(v, x);
                    break;
                case "2":
                    result = Ceiling(v, x);
                    break;
                case "3":
                    result = Floor(v, x);
                    break;
                case "4":
                    result = SeeDownUp(v, x);
                    break;
                default:
                    result = Round(v, x);
                    break;
            }

            return result;
        }

 

以上是关于c# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数的主要内容,如果未能解决你的问题,请参考以下文章

PHP取整四舍五入取整向上取整向下取整小数截取

PHP取整,四舍五入取整向上取整向下取整小数截取

向上取整与向下取整

js只保留整数,向上取整,四舍五入,向下取整等函数

python 向下取整,向上取整,四舍五入

js 向上取整向下取整四舍五入