标签:ring tostring mat ram modify param abs style static
/// <summary> /// 数据修约 -- 四舍六入5成双,至少保留一位有效位 /// 数值0.00512,保留2位,修约为0.01 /// </summary> /// <param name="modifyValue">原始数据</param> /// <param name="degit">保留位数</param> /// <returns></returns> public static string CalcValue(decimal modifyValue, int? degit) { decimal calValue = 0; int degitNotNull = degit ?? 2; calValue = Math.Round(modifyValue, degitNotNull); //按此修约为0则至少保留1位有效位数 if (calValue == 0 && modifyValue != 0) { return modifyValue.ToString("G1"); } else { return calValue.ToString("F" + degitNotNull); } }
/// <summary> /// 数据修约 -- 四舍六入5成双,至少保留一位有效位 /// 数值0.00512,保留2位,修约为0.005 /// </summary> /// <param name="modifyValue">原始数据</param> /// <param name="degit">保留位数</param> /// <returns></returns> public static string CalcValue2(decimal modifyValue, int? degit) { decimal calValue = 0; int degitNotNull = degit ?? 2; if (Math.Abs(modifyValue) >= 1) { calValue = Math.Round(modifyValue, degitNotNull); } //按此修约为0则至少保留1位有效位数 if (calValue == 0 && modifyValue != 0) { string value = modifyValue.ToString(); //从小数点后 value = value.Substring(value.IndexOf(".") + 1); for (int i = 0; i < value.Length; i++) { if ((i + 1) < degitNotNull) { calValue = Math.Round(modifyValue, degitNotNull); } else { calValue = Math.Round(modifyValue, (i + 1)); } } return calValue.ToString(); } else { return calValue.ToString("F" + degitNotNull); } }
标签:ring tostring mat ram modify param abs style static
原文地址:https://www.cnblogs.com/elves/p/10829767.html