人民币大写金额转换C#方法_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 人民币大写金额转换C#方法

人民币大写金额转换C#方法

 2013/12/5 15:26:00  roucheng  博客园  我要评论(0)
  • 摘要:方法的代码如下:1///<summary>2///人民币大写3///</summary>4///<paramname="input">待转换输入</param>5///<paramname="prefix">需要添加人民币前缀</param>6///<exceptioncref="ArgumentException"/>7///<returns>转换后的结果</returns>
  • 标签:C# 方法

方法的代码如下:

 1 /// <summary>
 2 /// 人民币大写
 3 /// </summary>
 4 /// <param name="input">待转换输入</param>
 5 /// <param name="prefix">需要添加人民币前缀</param>
 6 /// <exception cref="ArgumentException" />
 7 /// <returns>转换后的结果</returns>
 8 public static string ToCapital(this string input, bool prefix = false) {
 9 
10     #region Step1 输入有效性验证
11 
12     if (!Regex.IsMatch(input, @"(?<=-|^)\d*\.?\d*$"))
13         throw new ArgumentException("错误的输入金额!");
14 
15     if (Regex.IsMatch(input, @"^\d{25,}"))
16         throw new ArgumentException("输入数据太大无法转换!");
17 
18     #endregion
19 
20     #region Step2 格式化为中间字符串
21 
22     var positionDic = new Dictionary<int, string> {
23         {0,"F"},{1,"J"},{2,"."},{3,"S"},{4,"B"},{5,"Q"},{6,"W"},{7,"SW"},{8,"BW"},{9,"QW"},
24         {10,"Y"},{11,"SY"},{12,"BY"},{13,"QY"},{14,"WY"},{15,"SWY"},{16,"BWY"},{17,"QWY"},{18,"YY"},
25         {19,"SYY"},{20,"BYY"},{21,"QYY"},{22,"WYY"},{23,"SWYY"},{24,"BWYY"},{25,"QWYY"}                
26     };
27 
28     input = Regex.Replace(input, @"^\.", "0.");
29 
30     var integerPart = Regex.Replace(input, @"^-|\..*$", "");
31     var _matchDecimal = Regex.Match(input, @"\.\d*$", RegexOptions.None);
32     var decimalPart = Regex.Replace(_matchDecimal.Success ? Convert.ToDouble(_matchDecimal.Value).ToString("0.00") : "00", @"0\.", "");
33 
34     var processStack = new Stack<string>();
35     var charsArray = (integerPart + decimalPart).Reverse<char>();
36     for (int i = 0; i < charsArray.Count(); i++) {
37         processStack.Push(string.Format("{0}{1}", charsArray.ElementAt(i), positionDic[i]));
38     }
39 
40     //符号处理 keleyi.com
41     if (Regex.IsMatch(input, "^-", RegexOptions.None)) {
42         processStack.Push("-");
43     }
44     if (prefix) {
45         processStack.Push("");
46     }
47 
48     var process = string.Empty;
49     while (processStack.Count > 0) {
50         process += processStack.Pop();
51     }
52     //语义处理模式队列
53     Queue<Tuple<string, string, MatchEvaluator>> patterns = new Queue<Tuple<string, string, MatchEvaluator>>();
54     var patternBuilder = new StringBuilder();
55     for (int i = 3; i < positionDic.Count; i++) {
56         patternBuilder.AppendFormat("{0}{1}", (i == 3 ? "(0(?:" : "") + positionDic[i], i == positionDic.Count - 1 ? ")+?)+" : "|");
57     }
58     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(patternBuilder.ToString(), "0", null));
59     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?YY)+", null, m => m.Value.Replace("YY", "") + "YY"));
60     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?Y)+", null, m => m.Value.Replace("Y", "") + "Y"));
61     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:Q|B|S)?\d?W)+", null, m => m.Value.Replace("W", "") + "W"));
62     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?!^)0+\.", ".", null));
63     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0\.0J|^0\.", "", null));
64     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("0J|0F", "0", null));
65     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("J0?$", "JZ", null));
66     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"\.$|\.0+$", ".Z", null));
67     patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0+$|^[^.]{0}$", "0.Z", null));
68 
69     while (patterns.Count > 0) {
70         var pattern = patterns.Dequeue();
71         if (pattern.Item3 != null) {
72             process = Regex.Replace(process, pattern.Item1, pattern.Item3);
73         }
74         else {
75             process = Regex.Replace(process, pattern.Item1, pattern.Item2);
76         }
77     }
78 
79     #endregion
80 
81     #region Step3 翻译中间字符串
82     StringBuilder result = new StringBuilder();
83     var translatorDic = new Dictionary<char, string> {
84         {'0',""},{'1',""},{'2',""},{'3',""},{'4',""},{'5',""},{'6',""},{'7',""},{'8',""},{'9',""},
85         {'S',""},{'B',""},{'Q',""},{'W',""},{'Y',"亿"},
86         {'',"人民币"},{'-',""},{'.',""},{'J',""},{'F',""},{'Z',""}
87     };
88     for (int i = 0; i < process.Length; i++) {
89         result.Append(translatorDic[process[i]]);
90     }
91     #endregion
92 
93     return result.ToString();
94 }

http://www.cnblogs.com/roucheng/

一个人民币大写的扩展方法。感觉还是有点复杂,请朋友们帮忙优化下或是给个思路。

发表评论
用户名: 匿名