把数字转化成汉字
来源:互联网 发布:剪裁图片大小的软件 编辑:程序博客网 时间:2024/05/16 17:33
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { public static Dictionary<int, string> numberTable = new Dictionary<int, string>(); public static Dictionary<int, string> digitTable = new Dictionary<int, string>(); static List<string> list = new List<string>(); //是否有连续个零 static bool flag = false; static void Main(string[] args) { start(); while (true) { string str = Console.ReadLine(); int result = 0; int.TryParse(str, out result); IntChangeHanzi(result); for (int i = list.Count - 1; i >= 0; i--) { Console.Write(list[i]); } Console.WriteLine(); list.Clear(); } } static void IntChangeHanzi(int number) { string s = number.ToString(); int len = s.Length; //零做单独的处理 if (len == 1 && number == 0) { list.Add("零"); } for( int i = 1;i<=len;i++) { int digit = number % 10; number = number /10; if (i == 1) { if (digit != 0) { list.Add(numberTable[digit]); } else { flag = true; } } else { if (digit != 0) { list.Add(digitTable[i]); list.Add(numberTable[digit]); flag = false; } else { if (flag == false) { list.Add(numberTable[digit]); flag = true; } } } } } static void start() { numberTable.Add(0, "零"); numberTable.Add(1, "一"); numberTable.Add(2, "二"); numberTable.Add(3, "三"); numberTable.Add(4, "四"); numberTable.Add(5, "五"); numberTable.Add(6, "六"); numberTable.Add(7, "七"); numberTable.Add(8, "八"); numberTable.Add(9, "九"); digitTable.Add(1, "零"); digitTable.Add(2, "十"); digitTable.Add(3, "百"); digitTable.Add(4, "千"); digitTable.Add(5, "万"); digitTable.Add(6, "十"); digitTable.Add(7, "百"); digitTable.Add(8, "千"); digitTable.Add(9, "亿"); } }}