⾦额⼤⼩写转换
刚开通博客也不知道写什么,看博客太空了,就把⼀个项⽬中⾦额⼤⼩写转换的类抽出来。废话不说上图上代码。先看个demo
⽬前只能精确到分
主要代码如下
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5
6namespace⼤⼩写转换
7 {
8public class ConvertHelper
9 {
10///<summary>
11///将⼩写数字转成⼤写⾦额
12///</summary>
13///<param name="str"></param>
14///<returns></returns>
15public string ConvertNum(string str)
16 {
17string rstr = "";
猫吐了怎么回事18if (!IsPositveDecimal(str))
兰州旅游景点大全介绍
19 {
20 rstr = "负";
21 str = str.Substring(1);
22 }
23//return "输⼊的不是正数字!";
24if (Double.Par(str) > 999999999999.99)
25return"数字太⼤,⽆法换算,请输⼊⼀万亿元以下的⾦额";
26char[] ch = new char[1];
27 ch[0] = '.'; //⼩数点
28string[] splitstr = null; //定义按⼩数点分割后的字符串数组
29 splitstr = str.Split(ch[0]); //按⼩数点分割字符串
30if (splitstr.Length == 1) //只有整数部分
31return ConvertData(str) + "圆整";
32el//有⼩数部分
33 {
34
35 rstr += ConvertData(splitstr[0]) + "圆";//转换整数部分
36 rstr += ConvertXiaoShu(splitstr[1]); //转换⼩数部分
37return rstr;
38 }
39 }
40
41
42///
43///判断是否是正数字字符串
44///
45///判断字符串
46///如果是数字,返回true,否则返回fal
47public bool IsPositveDecimal(string str)
48 {
49 Decimal d;
50try
51 {
52 d = Decimal.Par(str);
53 }
54catch (Exception)
55 {
56return fal;
57 }
58if (d >= 0)
59return true;
60el
61return fal;
62 }
63
64
65///
66///转换数字(整数)
67///
68///需要转换的整数数字字符串
69///转换成中⽂⼤写后的字符串
70public string ConvertData(string str)
71 {
72string tmpstr = "";
73string rstr = "";
74int strlen = str.Length;
75if (strlen <= 4)//数字长度⼩于四位
76 {
77 rstr = ConvertDigit(str);
78
79 }
80el
81 {
82if (strlen <= 8)//数字长度⼤于四位,⼩于⼋位
83 {
84 tmpstr = str.Substring(strlen - 4, 4); //先截取最后四位数字
85 rstr = ConvertDigit(tmpstr);//转换最后四位数字
86 tmpstr = str.Substring(0, strlen - 4);//截取其余数字
87//将两次转换的数字加上万后相连接
88 rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
89 rstr = rstr.Replace("零零", "零");
90 }
91el
92if (strlen <= 12)//数字长度⼤于⼋位,⼩于⼗⼆位
93 {
94 tmpstr = str.Substring(strlen - 4, 4); //先截取最后四位数字
95 rstr = ConvertDigit(tmpstr);//转换最后四位数字
96 tmpstr = str.Substring(strlen - 8, 4); //再截取四位数字
97 rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
98 tmpstr = str.Substring(0, strlen - 8);
99 rstr = String.Concat(ConvertDigit(tmpstr) + "亿", rstr); 100 rstr = rstr.Replace("零亿", "亿");
101 rstr = rstr.Replace("零万", "零");
102 rstr = rstr.Replace("零零", "零");
103 rstr = rstr.Replace("零零", "零");
104 }
105 }
玉阶怨106 strlen = rstr.Length;
107if (strlen >= 2)
108 {
109switch (rstr.Substring(strlen - 2, 2))
110 {
111ca"佰零": rstr = rstr.Substring(0, strlen - 2) + "佰"; break; 112ca"仟零": rstr = rstr.Substring(0, strlen - 2) + "仟"; break; 113ca"万零": rstr = rstr.Substring(0, strlen - 2) + "万"; break; 114ca"亿零": rstr = rstr.Substring(0, strlen - 2) + "亿"; break; 115 }
116 }
117return rstr;
118 }
119
120
121
122///
123///转换数字(⼩数部分)
124///
125///需要转换的⼩数部分数字字符串
126///转换成中⽂⼤写后的字符串
127public string ConvertXiaoShu(string str)
128 {
129int strlen = str.Length;
130string rstr;
131if (strlen == 1)
132 {
133 rstr = ConvertChine(str) + "⾓";
134return rstr;
135 }
136el
137 {
138string tmpstr = str.Substring(0, 1);
139 rstr = ConvertChine(tmpstr) + "⾓";
140 tmpstr = str.Substring(1, 1);
141 rstr += ConvertChine(tmpstr) + "分";
142 rstr = rstr.Replace("零分", "");
143 rstr = rstr.Replace("零⾓", "");
白云山多高
144return rstr;
145 }
146 }
147
148
149///
150///转换数字
151///
152///转换的字符串(四位以内)
153///
154public string ConvertDigit(string str)
155 {
156int strlen = str.Length;
157string rstr = "";
158switch (strlen)
159 {
160ca1: rstr = ConvertChine(str); break; 161ca2: rstr = Convert2Digit(str); break; 162ca3: rstr = Convert3Digit(str); break; 163ca4: rstr = Convert4Digit(str); break; 164 }
165 rstr = rstr.Replace("拾零", "拾");
166 strlen = rstr.Length;
167return rstr;
168 }
169
170
171///
172///转换四位数字
173///gucheng
174public string Convert4Digit(string str)
175 {
176string str1 = str.Substring(0, 1);
177string str2 = str.Substring(1, 1);
178string str3 = str.Substring(2, 1);
179string str4 = str.Substring(3, 1);
180string rstring = "";
181 rstring += ConvertChine(str1) + "仟";
182 rstring += ConvertChine(str2) + "佰";
183 rstring += ConvertChine(str3) + "拾";
184 rstring += ConvertChine(str4);
185 rstring = rstring.Replace("零仟", "零");
186 rstring = rstring.Replace("零佰", "零");
187 rstring = rstring.Replace("零拾", "零");
188 rstring = rstring.Replace("零零", "零");
189 rstring = rstring.Replace("零零", "零");
190 rstring = rstring.Replace("零零", "零");
191return rstring;
192 }
193
194///
195///转换三位数字
196///
197public string Convert3Digit(string str)
肠胃虚弱怎样调理
198 {
199string str1 = str.Substring(0, 1);
200string str2 = str.Substring(1, 1);
201string str3 = str.Substring(2, 1);
202string rstring = "";
203 rstring += ConvertChine(str1) + "佰";
204 rstring += ConvertChine(str2) + "拾";
205 rstring += ConvertChine(str3);
206 rstring = rstring.Replace("零佰", "零");
207 rstring = rstring.Replace("零拾", "零");
208 rstring = rstring.Replace("零零", "零");
209 rstring = rstring.Replace("零零", "零");
210return rstring;
211 }
212
213
214///
215///转换⼆位数字
216///
217public string Convert2Digit(string str)
218 {
219string str1 = str.Substring(0, 1);
220string str2 = str.Substring(1, 1);
221string rstring = "";
222 rstring += ConvertChine(str1) + "拾";
223 rstring += ConvertChine(str2);
224 rstring = rstring.Replace("零拾", "零");
225 rstring = rstring.Replace("零零", "零");
226return rstring;
227 }
228
229
230///
231///将⼀位数字转换成中⽂⼤写数字
232///
233public string ConvertChine(string str)
234 {
235//"零壹贰叁肆伍陆柒捌玖拾佰仟万亿圆整⾓分" 236string cstr = "";
推背的好处
起床用英语怎么写
237switch (str)
238 {
239ca"0": cstr = "零"; break;
240ca"1": cstr = "壹"; break;
241ca"2": cstr = "贰"; break;
242ca"3": cstr = "叁"; break;
243ca"4": cstr = "肆"; break;
244ca"5": cstr = "伍"; break;
245ca"6": cstr = "陆"; break;
246ca"7": cstr = "柒"; break;
247ca"8": cstr = "捌"; break;
248ca"9": cstr = "玖"; break;
249 }
250return (cstr);
251 }
252 }
253
254 }
直接调⽤ConvertNum()⽅法传⼈⾦额即可。菜鸟的第⼀篇⽂章写完了