码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-12-17 16:55:54      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:int   取整   style   col   向上取整   def   i++   process   负数   

/// <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# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数

标签:int   取整   style   col   向上取整   def   i++   process   负数   

原文地址:https://www.cnblogs.com/yxcn/p/10132194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!