最近在做一個項目的台灣本地化,需要用到新台幣的金額大寫。用 .NET 實現了轉換金額大寫的 RMB(人民幣)和 NTD(新台幣)方法。

經過測試,該方法很可靠。

注意:轉換出的結果為台灣正體。如果需要使用人民幣的大陸簡體大寫寫法,需要轉換為簡體即可。單擊此處瞭解如何進行簡繁轉換。

        /// <summary>
        /// 將新台幣或人民幣轉換為金額大寫形式。
        /// </summary>
        /// <param name="d" />要轉換的數值</param>
        /// <returns>返回轉換的結果。</returns>
        public static string GetMoneyUpper(decimal d, MoneyType moneyType)
        {
            string o = d.ToString();
            string dq = "", dh = "";
            if (o.Contains("."))
            {
                dq = o.Split('.')[0];
                dh = o.Split('.')[1];
            }
            else
            {
                dq = o;
            }
            string ret = GetMoney(dq, true, "圓", moneyType) + GetMoney(dh, false, "", moneyType);
            if (ret.Contains("厘") || ret.Contains("分"))
                return cc.ToTraditional(ret);
            if (moneyType != MoneyType.NTD)
            {
                ret = ret.Replace("參", "叁");
            }
            return cc.ToTraditional(ret + "整");
        }
        private static string GetMoney(string number, bool left, string lastdw, MoneyType type) // 內部函數。
        {
            string[] NTD  = new string[10] { "零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖" };
            string[] DW = new string[8] { "厘", "分", "角", "", "拾", "佰", "仟", "萬" };
            int first = 4;
            string str = "";
            if (!left)
            {
                first = 1;
                if (number.Length == 1)
                {
                    number += "00";
                }
                else if (number.Length == 2)
                {
                    number += "0";
                }
                else number = number.Substring(0, 3);
 
            }
            else
            {
                if (number.Length >= 9)
                {
                    return GetMoney(number.Substring(0, number.Length - 8), true, "億", type) + GetMoney(number.Substring(number.Length - 8, 8), true, "圓", type);
                }
                if (number.Length >= 5)
                {
                    return GetMoney(number.Substring(0, number.Length - 4), true, "萬", type) + GetMoney(number.Substring(number.Length - 4, 4), true, "圓", type);
                }
            }
            bool has0 = false;
            for (int i = 0; i < number.Length; ++i)
            {
                int w = number.Length - i + first - 2;
                if (int.Parse(number[i].ToString()) == 0)
                {
                    has0 = true;
                    continue;
                }
                else
                {
                    if (has0)
                    {
                        if (type == MoneyType.RMB)
                            str += "零";
                        has0 = false;
                    }
                }
                str += NTD [int.Parse(number[i].ToString())];
                str += DW[w];
            }
            if (left)
                str += lastdw;
            return str;
        }

    public enum MoneyType
    {
        /// <summary>
        /// 表示新台幣。
        /// </summary>
        NTD,
        /// <summary>
        /// 表示人民幣。
        /// </summary>
        RMB
    }