CFXIXI工作室首页
CF西西的博客 | 【.Net】如何截取2位小数(不用四舍五入)

【.Net】如何截取2位小数(不用四舍五入)

12. 六月 2015

两个方法参考:

 

        /// <summary>
        /// 不四舍五入获得浮动收益
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static decimal FormatFloatProfit(decimal amount)
        {
            decimal absValue = Math.Abs(amount);//绝对值
            decimal flag = absValue / amount;//正负数标记
            return Math.Floor(absValue * 100) / 100 * flag;
        }
/// <summary>
        /// 转换string为decimal
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(string d)
        {
            decimal result = default(decimal);
            if (decimal.TryParse(d, out result))
                return result;
            else
                return default(decimal);
        }

 

/// <summary>
        /// 格式化decimal
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string GetFormatString(decimal d)
        {
            return (Math.Floor(d * 100) / 100).ToString("0.00");
        }

.NET