zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    DOS命令大全 1 attrib,delete等 外部 内部 命令都有
    css图像映射
    简单的css样式控制分页效果
    (转)AjaxPro实现机制探讨——Ajax是如何调用服务器端C#方法?
    学习笔记ADO.Net方面
    一个挺好用的数据库操作类
    .net开发人员应该知道(二)
    js与C#之间相互调用的一些方法
    JS 事件大全
    .net开发人员应该知道(一)
  • 原文地址:https://www.cnblogs.com/yxcn/p/10132194.html
Copyright © 2011-2022 走看看