在C#中将数字转换成中文
上篇我们讲了在MSSQL中将数字转换成中文,这篇我们讲讲在C#中将数字转换成中文
下篇将讲一下如何将金额转换成中文金额,废话不多说,具体代码如下:
/// <summary> /// 数字转中文 /// </summary> /// <param name="number">eg: 22</param> /// <returns></returns> public string NumberToChinese(int number) { string res = string.Empty; string str = number.ToString(); string schar = str.Substring(0, 1); switch (schar) { case "1": res = "一"; break; case "2": res = "二"; break; case "3": res = "三"; break; case "4": res = "四"; break; case "5": res = "五"; break; case "6": res = "六"; break; case "7": res = "七"; break; case "8": res = "八"; break; case "9": res = "九"; break; default: res = "零"; break; } if (str.Length > 1) { switch (str.Length) { case 2: case 6: res += "十"; break; case 3: case 7: res += "百"; break; case 4: res += "千"; break; case 5: res += "万"; break; default: res += ""; break; } res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1))); } return res; }
使用:
Console.WriteLine(NumberToChinese(22));
结果:
二十二