C# 金额数字转中文的方法

        /// <summary>
        /// 金额数字转大写(带小数点)
        /// </summary>
        public static string PriceToCn(decimal price)
        {
            //数字转大写
            string[] n = { "", "", "", "", "", "", "", "", "", "" };
            string[] d = { "", "", "", "", "", "", "", "", "", "", "", "亿" };

            //不同位置的数字要加单位
            List<string> needReplace = new List<string> { "零拾", "零佰", "零仟", "零万", "零亿", "亿万", "零元", "零零", "零角", "零分" };
            List<string> afterReplace = new List<string> { "", "", "", "", "亿", "亿", "", "", "", "" };

            string e = price % 1 == 0 ? "" : "";  //金额是整数,加一个“整”结尾
            string re = "";
            int a = (int)(price * 100);
            int k = 1;
            while (a != 0)
            {
                //初步转换大小写
                re = n[a % 10] + d[k] + re;
                a = a / 10;
                k = k < 11 ? k + 1 : 4;
            }

            string need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>();
            while (need != null)
            {
                int i = needReplace.IndexOf(need);
                re = re.Replace(needReplace[i], afterReplace[i]);
                need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>();
            }//循环排除特殊情况
            re = re == "" ? "零元" : re + e;

            return re;
        }

 

posted @ 2021-09-17 13:46  Nine_Jason  阅读(477)  评论(2编辑  收藏  举报