/// <summary>
        /// decimal保留指定位数小数 不四舍五入
        /// </summary>
        /// <param name="num">原始数量</param>
        /// <param name="scale">保留小数位数,0为取整数</param>
        /// <returns>截取指定小数位数后的数量字符串</returns>
        public static decimal DecimalExtension(decimal num, int scale = 0)
        {
            string numToString = num.ToString();

            int index = numToString.IndexOf(".");
            int length = numToString.Length;

            if (index != -1)
            {
                string str = string.Format("{0}.{1}",
                    numToString.Substring(0, index),//整数部分
                    numToString.Substring(index + 1, Math.Min(length - index - 1, scale)));
                return decimal.Parse(str);
            }
            else
            {
                return decimal.Parse(num.ToString());
            }

 

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐