标签:
动手写一个转换日期的小方法,虽然很短,但是需要考虑的东西还是挺多的,记录一下。
/// <summary> /// 将日期转换为中文大写 /// 如:一九八三 十一 二十七 /// </summary> public class ChineseNumberHelper { static Dictionary<int, string> _theNumOfChineseCapital = new Dictionary<int, string>() { {0,"〇"}, {1,"一"}, {2,"二"}, {3,"三"}, {4,"四"}, {5,"五"}, {6,"六"}, {7,"七"}, {8,"八"}, {9,"九"}, {10,"十"} }; public static string ConverToString(object number, NumberType numType) { var input = Convert.ToString(number); int validateInt; if (string.IsNullOrWhiteSpace(input) || !int.TryParse(input, out validateInt)) throw new ArgumentException("number"); if (validateInt == 0) throw new ArgumentException("number"); //更多验证 如果是年 月 日的输入 if ((numType == NumberType.Day || numType == NumberType.Day) && input.Length == 2) input = input.TrimStart(‘0‘); StringBuilder outPut = new StringBuilder(); switch (numType) { case NumberType.Year: for (int i = 0; i < input.Length; i++) { var intVal = Convert.ToInt32(input[i].ToString()); outPut.Append(_theNumOfChineseCapital[intVal]); } break; case NumberType.Moth: case NumberType.Day: if (validateInt < 10) { outPut.Append(_theNumOfChineseCapital[Convert.ToInt32(input[0].ToString())]); } else if (validateInt == 10) { outPut.Append(_theNumOfChineseCapital[validateInt]); } else { if (validateInt > 19) outPut.Append(_theNumOfChineseCapital[Convert.ToInt32(input[0].ToString())]); outPut.Append(_theNumOfChineseCapital[10]); if (input[1] != ‘0‘) outPut.Append(_theNumOfChineseCapital[Convert.ToInt32(input[1].ToString())]); } break; } return outPut.ToString(); } } public enum NumberType { Year, Moth, Day }
标签:
原文地址:http://www.cnblogs.com/clith/p/4228094.html