标签: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