代码
1 public class Money
2 {
3 /// <summary>
4 /// 要转换的数字
5 /// </summary>
6 private double j;
7 /// <summary>
8 ///
9 /// </summary>
10 private string[] NumChineseCharacter = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
11
12 public Money(double m)
13 {
14 this.j = m;
15 }
16
17 /// <summary>
18 /// 判断输入的数字是否大于double类型
19 /// </summary>
20 private bool IsNumber
21 {
22 get
23 {
24 if (j > double.MaxValue || j <= 0)
25 return false;
26 else
27 return true;
28 }
29 }
30 /// <summary>
31 /// 数字转换成大写汉字主函数
32 /// </summary>
33 /// <returns>返回转换后的大写汉字</returns>
34 public string Convert()
35 {
36 string bb = "";
37 if (IsNumber)
38 {
39 string str = j.ToString();
40 string[] Num = str.Split('.');
41 if (Num.Length == 1)
42 {
43 bb = NumberString(Num[0]) + "元整";
44 bb = bb.Replace("零零", "零");
45 }
46 else
47 {
48 bb = NumberString(Num[0]) + "元";
49 bb += FloatString(Num[1]);
50 bb = bb.Replace("零零", "零");
51 }
52 }
53 else
54 {
55 MessageBox.Show("你输入的数字格式不正确或不是数字!");
56 }
57 return bb;
58 }
59 /// <summary>
60 /// 小数位转换只支持两位的小数
61 /// </summary>
62 /// <param name="Num">转换的小数</param>
63 /// <returns>小数转换成汉字</returns>
64 private string FloatString(string Num)
65 {
66 string cc = "";
67 if (Num.Length > 2)
68 {
69 throw new FormatException("小数位数过多.");
70 }
71 else
72 {
73 string bb = ConvertString(Num);
74 int len = bb.IndexOf("零");
75 if (len != 0)
76 {
77 bb = bb.Replace("零", "");
78 if (bb.Length == 1)
79 {
80 cc = bb.Substring(0, 1) + "角整";
81 }
82 else
83 {
84 cc = bb.Substring(0, 1) + "角";
85 cc += bb.Substring(1, 1) + "分";
86 }
87 }
88 else
89 cc = bb + "分";
90 }
91 return cc;
92 }
93 /// <summary>
94 /// 判断数字位数以进行拆分转换
95 /// </summary>
96 /// <param name="Num">要进行拆分的数字</param>
97 /// <returns>转换成的汉字</returns>
98 private string NumberString(string Num)
99 {
100 string bb = "";
101 if (Num.Length <= 4)
102 {
103 bb = Convert4(Num);
104 }
105 else if (Num.Length > 4 && Num.Length <= 8)
106 {
107 bb = Convert4(Num.Substring(0, Num.Length - 4)) + "万";
108 bb += Convert4(Num.Substring(Num.Length - 4, 4));
109 }
110 else if (Num.Length > 8 && Num.Length <= 12)
111 {
112 bb = Convert4(Num.Substring(0, Num.Length - 8)) + "亿";
113 if (Convert4(Num.Substring(Num.Length - 8, 4)) == "")
114 if (Convert4(Num.Substring(Num.Length - 4, 4)) != "")
115 bb += "零";
116 else
117 bb += "";
118 else
119 bb += Convert4(Num.Substring(Num.Length - 8, 4)) + "万";
120 bb += Convert4(Num.Substring(Num.Length - 4, 4));
121 }
122 return bb;
123 }
124 /// <summary>
125 /// 四位数字的转换
126 /// </summary>
127 /// <param name="Num">准备转换的四位数字</param>
128 /// <returns>转换以后的汉字</returns>
129 private string Convert4(string Num)
130 {
131 string bb = "";
132 if (Num.Length == 1)
133 {
134 bb = ConvertString(Num);
135 }
136 else if (Num.Length == 2)
137 {
138 bb = ConvertString(Num);
139 bb = Convert2(bb);
140 }
141 else if (Num.Length == 3)
142 {
143 bb = ConvertString(Num);
144 bb = Convert3(bb);
145 }
146 else
147 {
148 bb = ConvertString(Num);
149 string cc = "";
150 string len = bb.Substring(0, 4);
151 if (len != "零零零零")
152 {
153 len = bb.Substring(0, 3);
154 if (len != "零零零")
155 {
156 bb = bb.Replace("零零零", "");
157 if (bb.Length == 1)
158 {
159 bb = bb.Substring(0, 1) + "仟";
160 }
161 else
162 {
163 if (bb.Substring(0, 1) != "零" && bb.Substring(0, 2) != "零")
164 cc = bb.Substring(0, 1) + "仟";
165 else
166 cc = bb.Substring(0, 1);
167 bb = cc + Convert3(bb.Substring(1, 3));
168 }
169 }
170 else
171 {
172 bb = bb.Replace("零零零", "零");
173 }
174 }
175 else
176 {
177 bb = bb.Replace("零零零零", "");
178 }
179 }
180 return bb;
181 }
182 /// <summary>
183 /// 将数字转换成汉字
184 /// </summary>
185 /// <param name="Num">需要转换的数字</param>
186 /// <returns>转换后的汉字</returns>
187 private string ConvertString(string Num)
188 {
189 string bb = "";
190 for (int i = 0; i < Num.Length; i++)
191 {
192 bb += NumChineseCharacter[int.Parse(Num.Substring(i, 1))];
193 }
194 return bb;
195 }
196 /// <summary>
197 /// 两位数字的转换
198 /// </summary>
199 /// <param name="Num">两位数字</param>
200 /// <returns>转换后的汉字</returns>
201 private string Convert2(string Num)
202 {
203 string bb = ""; string cc = "";
204 string len = Num.Substring(0, 1);
205 if (len != "零")
206 {
207 bb = Num.Replace("零", "");
208 if (bb.Length == 1)
209 {
210 cc = bb.Substring(0, 1) + "拾";
211 }
212 else
213 {
214 cc = bb.Substring(0, 1) + "拾";
215 cc += bb.Substring(1, 1);
216 }
217 }
218 else
219 cc = Num;
220 return cc;
221 }
222 /// <summary>
223 /// 三位数字的转换
224 /// </summary>
225 /// <param name="Num">三位数字</param>
226 /// <returns>转换后的汉字</returns>
227 private string Convert3(string Num)
228 {
229 string bb = ""; string cc = "";
230 string len = Num.Substring(0, 2);
231 if (len != "零零")
232 {
233 bb = Num.Replace("零零", "");
234 if (bb.Length == 1)
235 {
236 bb = bb.Substring(0, 1) + "佰";
237 }
238 else
239 {
240 if (bb.Substring(0, 1) != "零")
241 cc = bb.Substring(0, 1) + "佰";
242 else
243 cc = bb.Substring(0, 1);
244 bb = cc + Convert2(bb.Substring(1, 2));
245 }
246 }
247 else
248 {
249 bb = Num.Replace("零零", "零");
250 }
251 return bb;
252 }
253 }
254
255 //调用TextBox输入显示
256 double m = Convert.ToDouble(textBox1.Text);
257 Money money = new Money(m);
258 textBox2.Text = money.Convert().ToString();
2 {
3 /// <summary>
4 /// 要转换的数字
5 /// </summary>
6 private double j;
7 /// <summary>
8 ///
9 /// </summary>
10 private string[] NumChineseCharacter = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
11
12 public Money(double m)
13 {
14 this.j = m;
15 }
16
17 /// <summary>
18 /// 判断输入的数字是否大于double类型
19 /// </summary>
20 private bool IsNumber
21 {
22 get
23 {
24 if (j > double.MaxValue || j <= 0)
25 return false;
26 else
27 return true;
28 }
29 }
30 /// <summary>
31 /// 数字转换成大写汉字主函数
32 /// </summary>
33 /// <returns>返回转换后的大写汉字</returns>
34 public string Convert()
35 {
36 string bb = "";
37 if (IsNumber)
38 {
39 string str = j.ToString();
40 string[] Num = str.Split('.');
41 if (Num.Length == 1)
42 {
43 bb = NumberString(Num[0]) + "元整";
44 bb = bb.Replace("零零", "零");
45 }
46 else
47 {
48 bb = NumberString(Num[0]) + "元";
49 bb += FloatString(Num[1]);
50 bb = bb.Replace("零零", "零");
51 }
52 }
53 else
54 {
55 MessageBox.Show("你输入的数字格式不正确或不是数字!");
56 }
57 return bb;
58 }
59 /// <summary>
60 /// 小数位转换只支持两位的小数
61 /// </summary>
62 /// <param name="Num">转换的小数</param>
63 /// <returns>小数转换成汉字</returns>
64 private string FloatString(string Num)
65 {
66 string cc = "";
67 if (Num.Length > 2)
68 {
69 throw new FormatException("小数位数过多.");
70 }
71 else
72 {
73 string bb = ConvertString(Num);
74 int len = bb.IndexOf("零");
75 if (len != 0)
76 {
77 bb = bb.Replace("零", "");
78 if (bb.Length == 1)
79 {
80 cc = bb.Substring(0, 1) + "角整";
81 }
82 else
83 {
84 cc = bb.Substring(0, 1) + "角";
85 cc += bb.Substring(1, 1) + "分";
86 }
87 }
88 else
89 cc = bb + "分";
90 }
91 return cc;
92 }
93 /// <summary>
94 /// 判断数字位数以进行拆分转换
95 /// </summary>
96 /// <param name="Num">要进行拆分的数字</param>
97 /// <returns>转换成的汉字</returns>
98 private string NumberString(string Num)
99 {
100 string bb = "";
101 if (Num.Length <= 4)
102 {
103 bb = Convert4(Num);
104 }
105 else if (Num.Length > 4 && Num.Length <= 8)
106 {
107 bb = Convert4(Num.Substring(0, Num.Length - 4)) + "万";
108 bb += Convert4(Num.Substring(Num.Length - 4, 4));
109 }
110 else if (Num.Length > 8 && Num.Length <= 12)
111 {
112 bb = Convert4(Num.Substring(0, Num.Length - 8)) + "亿";
113 if (Convert4(Num.Substring(Num.Length - 8, 4)) == "")
114 if (Convert4(Num.Substring(Num.Length - 4, 4)) != "")
115 bb += "零";
116 else
117 bb += "";
118 else
119 bb += Convert4(Num.Substring(Num.Length - 8, 4)) + "万";
120 bb += Convert4(Num.Substring(Num.Length - 4, 4));
121 }
122 return bb;
123 }
124 /// <summary>
125 /// 四位数字的转换
126 /// </summary>
127 /// <param name="Num">准备转换的四位数字</param>
128 /// <returns>转换以后的汉字</returns>
129 private string Convert4(string Num)
130 {
131 string bb = "";
132 if (Num.Length == 1)
133 {
134 bb = ConvertString(Num);
135 }
136 else if (Num.Length == 2)
137 {
138 bb = ConvertString(Num);
139 bb = Convert2(bb);
140 }
141 else if (Num.Length == 3)
142 {
143 bb = ConvertString(Num);
144 bb = Convert3(bb);
145 }
146 else
147 {
148 bb = ConvertString(Num);
149 string cc = "";
150 string len = bb.Substring(0, 4);
151 if (len != "零零零零")
152 {
153 len = bb.Substring(0, 3);
154 if (len != "零零零")
155 {
156 bb = bb.Replace("零零零", "");
157 if (bb.Length == 1)
158 {
159 bb = bb.Substring(0, 1) + "仟";
160 }
161 else
162 {
163 if (bb.Substring(0, 1) != "零" && bb.Substring(0, 2) != "零")
164 cc = bb.Substring(0, 1) + "仟";
165 else
166 cc = bb.Substring(0, 1);
167 bb = cc + Convert3(bb.Substring(1, 3));
168 }
169 }
170 else
171 {
172 bb = bb.Replace("零零零", "零");
173 }
174 }
175 else
176 {
177 bb = bb.Replace("零零零零", "");
178 }
179 }
180 return bb;
181 }
182 /// <summary>
183 /// 将数字转换成汉字
184 /// </summary>
185 /// <param name="Num">需要转换的数字</param>
186 /// <returns>转换后的汉字</returns>
187 private string ConvertString(string Num)
188 {
189 string bb = "";
190 for (int i = 0; i < Num.Length; i++)
191 {
192 bb += NumChineseCharacter[int.Parse(Num.Substring(i, 1))];
193 }
194 return bb;
195 }
196 /// <summary>
197 /// 两位数字的转换
198 /// </summary>
199 /// <param name="Num">两位数字</param>
200 /// <returns>转换后的汉字</returns>
201 private string Convert2(string Num)
202 {
203 string bb = ""; string cc = "";
204 string len = Num.Substring(0, 1);
205 if (len != "零")
206 {
207 bb = Num.Replace("零", "");
208 if (bb.Length == 1)
209 {
210 cc = bb.Substring(0, 1) + "拾";
211 }
212 else
213 {
214 cc = bb.Substring(0, 1) + "拾";
215 cc += bb.Substring(1, 1);
216 }
217 }
218 else
219 cc = Num;
220 return cc;
221 }
222 /// <summary>
223 /// 三位数字的转换
224 /// </summary>
225 /// <param name="Num">三位数字</param>
226 /// <returns>转换后的汉字</returns>
227 private string Convert3(string Num)
228 {
229 string bb = ""; string cc = "";
230 string len = Num.Substring(0, 2);
231 if (len != "零零")
232 {
233 bb = Num.Replace("零零", "");
234 if (bb.Length == 1)
235 {
236 bb = bb.Substring(0, 1) + "佰";
237 }
238 else
239 {
240 if (bb.Substring(0, 1) != "零")
241 cc = bb.Substring(0, 1) + "佰";
242 else
243 cc = bb.Substring(0, 1);
244 bb = cc + Convert2(bb.Substring(1, 2));
245 }
246 }
247 else
248 {
249 bb = Num.Replace("零零", "零");
250 }
251 return bb;
252 }
253 }
254
255 //调用TextBox输入显示
256 double m = Convert.ToDouble(textBox1.Text);
257 Money money = new Money(m);
258 textBox2.Text = money.Convert().ToString();