[C#][WinForm]如何將數字轉為國字

  • 11232
  • 0
  • C#
  • 2009-12-07

[C#][WinForm]如何將數字轉為國字

網友問題,自己實做記錄。

利用array+substring馬上可輕鬆轉換成功。

image image

image image


private String NumberToString(String input)
        {
            String[] arr1 = new String[10] {"","一", "二", "三", "四", "五", "六", "七", "八", "九" };
            String[] arr2 = new String[3] {"","十", "百" };
            String result = null;
            if (input.Length > 3)
            {
                return "輸入錯誤";
            }
            for (Int32 i = 0; i < input.Length; i++)
            {
                if (input.Substring(i, 1) != "0") 
                {
                 if (i >1)
                    {
                        if (input.Substring(i-1, 1) == "0")
                        {
                            result = result + "零";
                        }                       
                    }
                    result = result + arr1[Convert.ToInt32(input.Substring(i, 1))];
                    result = result + arr2[(input.Length - (i+1)) % 3];//取餘數
                }
                else if (input.Length == 1 && input.Substring(i, 1) == "0")
                {
                    result = "零";
                }              
            }
            if (result.Length > 1 && result.Substring(0, 1) == "零")
            {
                result = result.Replace("零",null);
            }
            return result;
        }