人民币小写转大写金额(可达千百万亿)

在很多企业级的软件开发中,很多时候会涉及到人民币的小写转大写。原来有个VB6的程序,现转为.Net,实现相当简单,同时支持达千百万亿的金额转换。现贴出代码以供参考:(本思路有别于网上很多的代码,通读易懂,可说是最佳实现)

 

        /// <summary>
        /// 小写金额转换成大写金额
        /// </summary>
        /// <param name="number"></param>
        /// <param name="turnType"></param>
        /// <returns></returns>
        public static string ConvertToCHNMoney(decimal number, CHNMoneyType turnType)
        {

            string s_CHNNumString = "零壹贰叁肆伍陆柒捌玖";
            //可正确最大金额可以到千万亿元正,有分位为百万亿元或十万亿
            string result = string.Empty;
            if (number > 0)
            {
                string s_DXDw = "分角元拾佰仟万拾佰仟亿拾佰仟万拾佰仟";
                ulong je = (ulong)(number * 100);
                ulong temNum = je;
                int index = 0;
                for (int n = 0; temNum != 0; n++)
                {
                    index = (int)(temNum - temNum / 10 * 10); //取出最后一个数
                    temNum = temNum / 10;
                    result = s_CHNNumString.Substring(index, 1) + s_DXDw.Substring(n, 1) + result;    //发票格式
                }
                if (turnType == CHNMoneyType.CheckType)
                {
                    string temResult = string.Empty;
                    string temMoney = string.Empty;
                    string unit = string.Empty;
                    string digitChar = string.Empty;
                    string str1 = "零元万整";
                    string str2 = "零元万亿整";
                    for (int n = result.Length - 2; n >= 0; n = n - 2)  //从后面每二个取字符串。
                    {
                        temMoney = result.Substring(n, 2);
                        unit = temMoney.Substring(1, 1);
                        digitChar = temMoney.Substring(0, 1);
                        switch (unit)
                        {
                            case "分":
                                if (digitChar == "零")
                                    temMoney = "整";
                                break;
                            case "元":
                            case "万":
                            case "亿":
                                string temDigitChar = temResult.Substring(0, 1);
                                if (digitChar == "零")
                                {
                                    if (str1.IndexOf(temDigitChar) == -1)
                                        temMoney = unit + "零";
                                    else
                                    {
                                        if (temDigitChar == "万")
                                            temResult = temResult.Substring(1);
                                        temMoney = unit;
                                    }
                                }
                                else if (temDigitChar == "万")
                                    temResult = temResult.Substring(1);
                                break;
                            default:
                                if (digitChar == "零")
                                {
                                    if (str2.IndexOf(temResult.Substring(0, 1)) != -1)
                                        temMoney = "";
                                    else
                                        temMoney = "零";
                                }
                                break;
                        }
                        temResult = temMoney + temResult;
                    }
                    result = temResult;
                }
            }
            return result;
        }
    }

    /// <summary>
    /// 大字金额的格式。
    /// </summary>
    public enum CHNMoneyType
    {
        /// <summary>
        /// 支票格式
        /// </summary>
        CheckType,
        /// <summary>
        /// 发票格式
        /// </summary>
        InvoiceType
    }

 

posted @ 2011-03-16 03:14  Lance Yang  阅读(2570)  评论(3编辑  收藏  举报