C# 金钱 小写转大写的算法


调用 ConvertMoney的ConvertMoneyToWords(decimal money)方法即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Common
{
    public class ConvertMoney
    {
        static string[] c_Num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        static string[] c_FH = { "", "拾", "佰", "仟" };
        static string[] c_Wn = { "圆", "万", "亿" };
        public static string ConvertMoneyToWords(decimal money)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(money.ToString()))
            {
                string[] moneysplit = money.ToString().Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                Regex reg = new Regex("零{2,}");
                string m = moneysplit[0];
                int mlen = m.Length;
                string word = string.Empty;
                for (int i = mlen; i > 0; i--)
                {
                    word = ConverNumToWord(word, m[mlen - i], i, true);
                }
                if (moneysplit.Length > 1)
                {
                    string d = moneysplit[1];
                    int dlen = d.Length > 2 ? 2 : d.Length;
                    if (dlen == 2 && d[0] == '0' && d[1] == '0')
                    {
                        word += "整";
                    }
                    else
                    {
                        for (int i = 0; i < dlen; i++)
                        {
                            word = ConverNumToWord(word, d[i], i, false);
                        }
                    }
                }
                else
                {
                    word += "整";
                }
                result = reg.Replace(word.ToString(), "零");
            }
            return result;
        }
        private static string ConverNumToWord(string appendStr, char a, int index, bool isYuan)
        {
            string s = c_Num[Convert.ToInt32(a) - 48];
            if (isYuan)
            {
                int z = (index - 1) / 4;
                int y = (index - 1) % 4;
                appendStr = appendStr + s + (s != "零" ? c_FH[y] : "");
                if (y == 0)
                {
                    appendStr = appendStr.Trim('零') + c_Wn[z];
                }
            }
            else
            {
                if (index == 0 && s != "零")
                {
                    appendStr = appendStr + s + "角";
                }
                else if (index == 0)
                {
                    appendStr = appendStr + s;
                }
                if (index == 1 && s != "零")
                {
                    appendStr = appendStr + s + "分";
                }
            }
            return appendStr;
        }
    }
}
C