

1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using System.Text;
8
9
namespace Money
10

{
11
/**//// <summary>
12
/// Form1 的摘要说明。
13
/// </summary>
14
public class Form1 : System.Windows.Forms.Form
15
{
16
private System.Windows.Forms.Label label1;
17
private System.Windows.Forms.Button button1;
18
private System.Windows.Forms.TextBox textBox1;
19
private System.Windows.Forms.TextBox textBox2;
20
private System.Windows.Forms.Label label2;
21
/**//// <summary>
22
/// 必需的设计器变量。
23
/// </summary>
24
private System.ComponentModel.Container components = null;
25
26
public Form1()
27
{
28
//
29
// Windows 窗体设计器支持所必需的
30
//
31
InitializeComponent();
32
33
//
34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
35
//
36
}
37
38
/**//// <summary>
39
/// 清理所有正在使用的资源。
40
/// </summary>
41
protected override void Dispose( bool disposing )
42
{
43
if( disposing )
44
{
45
if (components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52
53
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
54
/**//// <summary>
55
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
56
/// 此方法的内容。
57
/// </summary>
58
private void InitializeComponent()
59
{
60
this.label1 = new System.Windows.Forms.Label();
61
this.button1 = new System.Windows.Forms.Button();
62
this.textBox1 = new System.Windows.Forms.TextBox();
63
this.textBox2 = new System.Windows.Forms.TextBox();
64
this.label2 = new System.Windows.Forms.Label();
65
this.SuspendLayout();
66
//
67
// label1
68
//
69
this.label1.Location = new System.Drawing.Point(56, 64);
70
this.label1.Name = "label1";
71
this.label1.Size = new System.Drawing.Size(136, 23);
72
this.label1.TabIndex = 0;
73
this.label1.Text = "小写";
74
//
75
// button1
76
//
77
this.button1.Location = new System.Drawing.Point(56, 152);
78
this.button1.Name = "button1";
79
this.button1.TabIndex = 2;
80
this.button1.Text = "转换";
81
this.button1.Click += new System.EventHandler(this.button1_Click);
82
//
83
// textBox1
84
//
85
this.textBox1.Location = new System.Drawing.Point(160, 64);
86
this.textBox1.Name = "textBox1";
87
this.textBox1.Size = new System.Drawing.Size(376, 21);
88
this.textBox1.TabIndex = 0;
89
this.textBox1.Text = "";
90
//
91
// textBox2
92
//
93
this.textBox2.Location = new System.Drawing.Point(160, 104);
94
this.textBox2.Name = "textBox2";
95
this.textBox2.ReadOnly = true;
96
this.textBox2.Size = new System.Drawing.Size(376, 21);
97
this.textBox2.TabIndex = 1;
98
this.textBox2.Text = "";
99
//
100
// label2
101
//
102
this.label2.Location = new System.Drawing.Point(56, 104);
103
this.label2.Name = "label2";
104
this.label2.Size = new System.Drawing.Size(120, 23);
105
this.label2.TabIndex = 3;
106
this.label2.Text = "大写";
107
//
108
// Form1
109
//
110
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
111
this.ClientSize = new System.Drawing.Size(600, 325);
112
this.Controls.Add(this.textBox2);
113
this.Controls.Add(this.label2);
114
this.Controls.Add(this.textBox1);
115
this.Controls.Add(this.button1);
116
this.Controls.Add(this.label1);
117
this.Name = "Form1";
118
this.Text = "Form1";
119
this.ResumeLayout(false);
120
121
}
122
#endregion
123
124
/**//// <summary>
125
/// 应用程序的主入口点。
126
/// </summary>
127
[STAThread]
128
static void Main()
129
{
130
Application.Run(new Form1());
131
}
132
133
private void button1_Click(object sender, System.EventArgs e)
134
{
135
double input = 0 ;
136
String strMoney = null;
137
String strinput = null;
138
139
//接受输入
140
strinput = textBox1.Text.Trim();
141
try
142
{
143
input = Convert.ToDouble(strinput);
144
if(input < 0 || input > 100000000000)
145
{
146
throw new Exception();
147
}
148
strMoney = convMoney( input);
149
textBox2.Text = strMoney + "整";
150
}
151
catch(Exception)
152
{
153
MessageBox.Show("请输入合适的数字!");
154
textBox1.Text = "";
155
textBox2.Text = "";
156
return;
157
}
158
}
159
160
//转换方法
161
private string convMoney(double input)
162
{
163
string result = "";
164
string tmpResult;
165
input *= 100;
166
input = Convert.ToUInt64(input);
167
tmpResult = input.ToString();
168
for(int i=0; i< tmpResult.Length; i++)
169
{
170
string tmpChar = tmpResult.Substring(tmpResult.Length-1-i,1);
171
if(i==0 && tmpChar=="0")
172
{
173
continue;
174
}
175
else if(i==1 && tmpChar=="0")
176
{
177
continue;
178
}
179
result = Upper(tmpChar) + Unit(i) + result;
180
}
181
StringBuilder sb = new StringBuilder();
182
sb.Append(result.Substring(0,1));
183
for(int m=1; m<result.Length; m++)
184
{
185
if(result.Substring(m,1) != result.Substring(m-1,1))
186
{
187
sb.Append(result.Substring(m,1));
188
}
189
}
190
result = sb.ToString();
191
192
if(result.Substring(result.Length-1,1)=="零")
193
{
194
sb.Replace("零","元",result.Length-1,1);
195
}
196
else if((result.Substring(result.Length-1,1)=="角" && result.Substring(result.Length-3,1)=="零") || (result.Substring(result.Length-1,1)=="分" && result.Substring(result.Length-3,1)=="零"))
197
{
198
sb.Replace("零","元",result.Length-3,1);
199
}
200
else if(result.Substring(result.Length-1,1)=="分" && result.Substring(result.Length-3,1)=="角" && result.Substring(result.Length-5,1)=="零")
201
{
202
sb.Replace("零","元",result.Length-5,1);
203
}
204
result = sb.ToString();
205
result = result.Replace("零仟","零");
206
result = result.Replace("零佰","零");
207
result = result.Replace("零拾","零");
208
while(true)
209
{
210
if(result.IndexOf("零零") == -1)
211
{
212
break;
213
}
214
result = result.Replace("零零","零");
215
}
216
result = result.Replace("零亿","亿");
217
result = result.Replace("零万","万");
218
result = result.Replace("零元","元");
219
result = result.Replace("亿万","亿");
220
return result;
221
}
222
223
//小写转换为大写
224
private string Upper(string strBefore)
225
{
226
string strAfter = null;
227
switch(strBefore)
228
{
229
case "0":
230
strAfter = "零";
231
break;
232
case "1":
233
strAfter = "壹";
234
break;
235
case "2":
236
strAfter = "贰";
237
break;
238
case "3":
239
strAfter = "叁";
240
break;
241
case "4":
242
strAfter = "肆";
243
break;
244
case "5":
245
strAfter = "伍";
246
break;
247
case "6":
248
strAfter = "陆";
249
break;
250
case "7":
251
strAfter = "柒";
252
break;
253
case "8":
254
strAfter = "捌";
255
break;
256
case "9":
257
strAfter = "玖";
258
break;
259
}
260
return strAfter;
261
}
262
263
//得到货币单位
264
private string Unit(int i)
265
{
266
string strUnit = "";
267
if(i==0)
268
{
269
strUnit = "分";
270
}
271
else if(i==1)
272
{
273
strUnit = "角";
274
}
275
else
276
{
277
i -= 2;
278
if(i/4 == 0 && i%4 == 0)
279
{
280
strUnit = "元";
281
}
282
else if(i/4 == 1 && i%4 == 0)
283
{
284
strUnit = "万";
285
}
286
else if(i/4 == 2 && i%4 == 0)
287
{
288
strUnit = "亿";
289
}
290
else if(i/4 > 2 && i%4 == 0)
291
{
292
for(int j = 0; j<i/4; j++)
293
{
294
strUnit += "万";
295
}
296
strUnit += "亿";
297
}
298
else
299
{
300
strUnit = getBit(i%4);
301
}
302
}
303
return strUnit;
304
}
305
306
//得到位数
307
private string getBit(int i)
308
{
309
string strBit = "";
310
switch(i)
311
{
312
case 1:
313
strBit = "拾";
314
break;
315
case 2:
316
strBit = "佰";
317
break;
318
case 3:
319
strBit = "仟";
320
break;
321
}
322
return strBit;
323
}
324
}
325
}
326
点击右上角即可分享
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步