• .NET实现把阿拉伯数字转换成中文大写文字的方法
  • 发布于 2个月前
  • 156 热度
    0 评论

最近在做一个财务软件,需要把阿拉伯数字的金额转换成中文,比如把金额325.2转换成叁佰贰拾伍元贰角这样显示,最近研究了,现在把实现方式写出来,希望对有类似数字转汉字需求的童鞋有帮助。

直接上代码:

    /// <summary>
        /// 金额转换成大写
        /// </summary>
        /// <param name="ANumber"></param>
        /// <returns></returns>
        public static string MoneyCn(double ANumber)
        {
           
            if (ANumber == 0) return "零";
            string Result = NumberCn(Math.Truncate(ANumber * 100) / 100);
            Result = Result.Replace("一", "壹");
            Result = Result.Replace("二", "贰");
            Result = Result.Replace("三", "叁");
            Result = Result.Replace("四", "肆");
            Result = Result.Replace("五", "伍");
            Result = Result.Replace("六", "陆");
            Result = Result.Replace("七", "柒");
            Result = Result.Replace("八", "捌");
            Result = Result.Replace("九", "玖");
            Result = Result.Replace("九", "玖");
            Result = Result.Replace("十", "拾");
            Result = Result.Replace("百", "佰");
            Result = Result.Replace("千", "仟");
            if (Result.Contains("点"))
            {
                int P = Result.IndexOf("点");
              
                Result = Result.Insert(P + 3, "分");
                Result = Result.Insert(P + 2, "角");
                Result = Result.Replace("点", "元");
                Result = Result.Replace("角分", "角");
                Result = Result.Replace("零分", "");
                Result = Result.Replace("零角", "");
                Result = Result.Replace("分角", "");
                if (Result.Substring(0, 2) == "零元")
                    Result = Result.Replace("零元", "");
            }
            else Result += "元整";
            Result = "人民币" + Result;
            return Result;
        }
        public static string NumberCn(double ANumber)
        {
            const string cPointCn = "点十百千万十百千亿十百千";
            const string cNumberCn = "零一二三四五六七八九";
            string S = string.Format("{0:f2}", ANumber);
            if (S == "0") return "" + cPointCn[0];
            if (!S.Contains(".")) S += ".";
            int P = S.IndexOf(".");
            string Result = "";

            for (int i = 0; i < S.Length; i++)
            {
                if (P == i)
                {
                    Result = Result.Replace("零十零", "零");
                    Result = Result.Replace("零百零", "零");
                    Result = Result.Replace("零千零", "零");
                    Result = Result.Replace("零十", "零");
                    Result = Result.Replace("零百", "零");
                    Result = Result.Replace("零千", "零");
                    Result = Result.Replace("零万", "万");
                    Result = Result.Replace("零亿", "亿");
                    Result = Result.Replace("亿万", "亿");
                    Result = Result.Replace("零点", "点");
                }
                else
                {
                    if (P > i)
                        Result += "" + cNumberCn[S[i] - '0'] + cPointCn[P - i - 1];
                    else Result += "" + cNumberCn[S[i] - '0'];
                }
            }
            if (Result.Substring(Result.Length - 1, 1) == "" + cPointCn[0])
                Result = Result.Remove(Result.Length - 1); // 一点-> 一

            if (Result[0] == cPointCn[0])
                Result = cNumberCn[0] + Result; // 点三-> 零点三

            if ((Result.Length > 1) && (Result[1] == cPointCn[1]) &&
               (Result[0] == cNumberCn[1]))
                Result = Result.Remove(0, 1); // 一十三-> 十三
            return Result;
        }
使用方式:

我上面的代码是直接放在CommonHelper类中的,所以调用时直接这样调用:

string cnDesc= CommonHelper.MoneyCn(345.23);


用户评论