阿拉伯數字轉漢字數字(C#)

using System;
using System.Collections.Generic;
using System.Text;


    public class NumToCharactor
    {
        /// <summary>
        /// 要轉換的數字
        /// </summary>
        private int j;
       
        /// <summary>
        ///
        /// </summary>
        private string[] Digitals = new string[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };

        private string[] Unit = new string[] { "", "十", "百", "千", "萬", "十", "百", "千", "億", "十", "百", "千", "萬" };


        public NumToCharactor()
        {
            this.j = 0;
        }

        public NumToCharactor(int m)
        {
            this.j = m;
        }


        /// <summary>
        /// 數字轉換成漢字主函數
        /// </summary>
        /// <returns>返回轉換後的漢字</returns>
        public string Convert(int num,bool btype)
        {
            char[] strNum = num.ToString().ToCharArray();
            string chsNum = "";
            if (btype == false)      //轉月 日
            {

                int high = strNum.Length - 1;
                int plus = 0;
                char zero = '0';
                for (int i = high; i >= 0; i--)
                {
                    if (plus == 4)
                        plus = 0;

                    if (zero == '0' && strNum[i] == '0')
                        strNum[i] = '-';
                    else
                        zero = strNum[i];

                    if (i < (i + plus) && i < (high - 3) && strNum[i] != '-' && strNum[i + plus] == '-')
                        strNum[i + plus] = '+';

                    if (plus == 1 && strNum[i] == '1' && i == 0)
                        strNum[i] = '+';

                    plus++;
                }
                if (strNum.Length == 1 && strNum[0] == '-')
                    strNum[0] = '0';
                int unit = 0;
                for (int j = high; j >= 0; j--)
                {
                    if (strNum[j] != '-')
                    {
                        if (strNum[j] == '+')
                            chsNum = Unit[unit] + chsNum;
                        else if (strNum[j] == '0')
                            chsNum = Digitals[Int32.Parse(strNum[j].ToString())] + chsNum;
                        else
                            chsNum = Digitals[Int32.Parse(strNum[j].ToString())] + Unit[unit] + chsNum;
                    }
                    unit++;
                }
            }
            else   //轉年份
            {
                int i = 0;
                for (i = 0; i < strNum.Length; i++)
                {
                    chsNum += Digitals[Int32.Parse(strNum[i].ToString())];
                }
            }
            return chsNum;
        }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章