Example #1
0
        /// <summary>
        /// 保留小数位
        /// </summary>
        /// <param name="obj">数量</param>
        /// <param name="len">保留小数位数</param>
        /// <param name="mathType">舍入类型</param>
        /// <returns></returns>
        public static decimal KeepDecimal(object obj, int len, MathType mathType = MathType.Round)
        {
            decimal result = Utils.ToDecimal(obj);

            switch (mathType)
            {
            case MathType.Floor:
                return(Math.Floor((decimal)Math.Pow(10, len) * result) / (decimal)Math.Pow(10, len));

            case MathType.Ceil:
                return(Math.Ceiling((decimal)Math.Pow(10, len) * result) / (decimal)Math.Pow(10, len));

            case MathType.Round:
            default:
                return(Math.Round(result, len, MidpointRounding.AwayFromZero));
            }
        }