金额转换为人民币大写(C#)
1
using System;
2
3
namespace TestUpperRMB
4
{
5
/// <summary>
6
/// 本类实现阿拉伯数字到大写中文的转换
7
/// 该类没有对非法数字进行判别,请事先自己判断数字是否合法
8
/// </summary>
9
public class ChineseNum
10
{
11
public static string GetChineseNum(string p_num)
12
{
13
ChineseNum cn = new ChineseNum();
14
15
return cn.NumToChn(p_num);
16
}
17
18
public static string GetUpperMoney(double p_Money)
19
{
20
ChineseNum cn = new ChineseNum();
21
22
return cn.GetMoneyChinese(p_Money);
23
}
24
25
//转换数字
26
private char CharToNum(char x)
27
{
28
string stringChnNames="零一二三四五六七八九";
29
string stringNumNames="0123456789";
30
return stringChnNames[stringNumNames.IndexOf(x)];
31
}
32
33
//转换万以下整数
34
private string WanStrToInt(string x)
35
{
36
string[] stringArrayLevelNames=new string[4] {"","十","百","千"};
37
string ret="";
38
int i;
39
for (i=x.Length-1;i>=0;i--)
40
if (x[i] == '0')
41
{
42
ret = CharToNum(x[i]) + ret;
43
}
44
else
45
{
46
ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
47
}
48
while ((i = ret.IndexOf("零零")) != -1)
49
{
50
ret = ret.Remove(i, 1);
51
}
52
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
53
{
54
ret = ret.Remove(ret.Length - 1, 1);
55
}
56
if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
57
{
58
ret = ret.Remove(0, 1);
59
}
60
return ret;
61
}
62
//转换整数
63
private string StrToInt(string x)
64
{
65
int len=x.Length;
66
string ret,temp;
67
if (len <= 4)
68
{
69
ret = WanStrToInt(x);
70
}
71
else if (len <= 8)
72
{
73
ret = WanStrToInt(x.Substring(0, len - 4)) + "万";
74
temp = WanStrToInt(x.Substring(len - 4, 4));
75
if (temp.IndexOf("千") == -1 && temp != "")
76
ret += "零" + temp;
77
else
78
ret += temp;
79
}
80
else
81
{
82
ret = WanStrToInt(x.Substring(0, len - 8)) + "亿";
83
temp = WanStrToInt(x.Substring(len - 8, 4));
84
if (temp.IndexOf("千") == -1 && temp != "")
85
{
86
ret += "零" + temp;
87
}
88
else
89
{
90
ret += temp;
91
}
92
ret += "万";
93
temp = WanStrToInt(x.Substring(len - 4, 4));
94
if (temp.IndexOf("千") == -1 && temp != "")
95
{
96
ret += "零" + temp;
97
}
98
else
99
{
100
ret += temp;
101
}
102
103
}
104
int i;
105
if ((i = ret.IndexOf("零万")) != -1)
106
{
107
ret = ret.Remove(i + 1, 1);
108
}
109
while ((i = ret.IndexOf("零零")) != -1)
110
{
111
ret = ret.Remove(i, 1);
112
}
113
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
114
{
115
ret = ret.Remove(ret.Length - 1, 1);
116
}
117
return ret;
118
}
119
//转换小数
120
private string StrToDouble(string x)
121
{
122
string ret="";
123
for (int i = 0; i < x.Length; i++)
124
{
125
ret += CharToNum(x[i]);
126
}
127
return ret;
128
}
129
130
private string NumToChn(string x)
131
{
132
if (x.Length == 0)
133
{
134
return "";
135
}
136
string ret="";
137
if (x[0]=='-')
138
{
139
ret="负";
140
x=x.Remove(0,1);
141
}
142
if (x[0].ToString() == ".")
143
{
144
x = "0" + x;
145
}
146
if (x[x.Length - 1].ToString() == ".")
147
{
148
x = x.Remove(x.Length - 1, 1);
149
}
150
if (x.IndexOf(".") > -1)
151
{
152
ret += StrToInt(x.Substring(0, x.IndexOf("."))) + "点" + StrToDouble(x.Substring(x.IndexOf(".") + 1));
153
}
154
else
155
{
156
ret += StrToInt(x);
157
}
158
return ret;
159
}
160
161
162
private string GetMoneyChinese(Double Money)
163
{
164
int i;
165
string mstrSource;
166
167
if (Money == 0)
168
{
169
return "";
170
}
171
mstrSource = Money.ToString("#0.00");
172
i = mstrSource.IndexOf(".");
173
if (i > 0) {mstrSource = mstrSource.Replace(".","");}
174
if (mstrSource.Substring(0,1) == "0") {mstrSource = mstrSource.Remove(0,1);}
175
176
mstrSource = NumstrToChinese(mstrSource);
177
if (mstrSource.Length == 0) {return "";}
178
//负
179
if (Money < 0)
180
{
181
mstrSource = "负" + mstrSource;
182
}
183
184
mstrSource=mstrSource.Replace("0","零");
185
mstrSource=mstrSource.Replace("1","壹");
186
mstrSource=mstrSource.Replace("2","贰");
187
mstrSource=mstrSource.Replace("3","叁");
188
mstrSource=mstrSource.Replace("4","肆");
189
mstrSource=mstrSource.Replace("5","伍");
190
mstrSource=mstrSource.Replace("6","陆");
191
mstrSource=mstrSource.Replace("7","柒");
192
mstrSource=mstrSource.Replace("8","捌");
193
mstrSource=mstrSource.Replace("9","玖");
194
mstrSource=mstrSource.Replace("M","亿");
195
mstrSource=mstrSource.Replace("W","万");
196
mstrSource=mstrSource.Replace("S","仟");
197
mstrSource=mstrSource.Replace("H","佰");
198
mstrSource=mstrSource.Replace("T","拾");
199
mstrSource=mstrSource.Replace("Y","圆");
200
mstrSource=mstrSource.Replace("J","角");
201
mstrSource=mstrSource.Replace("F","分");
202
if (mstrSource.Substring(mstrSource.Length-1, 1) != "分")
203
{
204
mstrSource = mstrSource + "整";
205
}
206
return mstrSource;
207
}
208
209
//金额转换
210
private string NumstrToChinese(string numstr)
211
{
212
int i;
213
int j;
214
string mstrChar;
215
string[] mstrFlag=new string[4];
216
string mstrReturn="";
217
bool mblnAddzero=false;
218
219
mstrFlag[0] = "";
220
mstrFlag[1] = "T";
221
mstrFlag[2] = "H";
222
mstrFlag[3] = "S";
223
224
for (i = 1;i<=numstr.Length;i++)
225
{
226
j = numstr.Length - i;
227
mstrChar = numstr.Substring(i-1,1);
228
if (mstrChar != "0" && j > 1) {mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2) % 4];}
229
if (mstrChar == "0" && mblnAddzero==false)
230
{
231
mstrReturn = mstrReturn + "0";
232
mblnAddzero = true;
233
}
234
if (j == 14)
235
{
236
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
237
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
238
else
239
{mstrReturn = mstrReturn + "W";}
240
}
241
if (j == 2)
242
{
243
if (mstrReturn.Substring(mstrReturn.Length-1,1) == "0")
244
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "Y0";}
245
else
246
{mstrReturn = mstrReturn + "Y";}
247
//元
248
}
249
if (j == 6)
250
{
251
if (mstrReturn.Length > 2)
252
{
253
if (mstrReturn.Substring(mstrReturn.Length-2) != "M0")
254
{
255
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
256
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
257
else
258
{mstrReturn = mstrReturn + "W";}
259
}
260
}
261
else
262
{
263
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
264
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
265
else
266
{mstrReturn = mstrReturn + "W";}
267
}
268
}
269
if (j == 10)
270
{
271
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
272
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "M0";}
273
else
274
{mstrReturn = mstrReturn + "M";}
275
}
276
if (j == 0 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "F";}
277
if (j == 1 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "J";}
278
if (mstrChar != "0") {mblnAddzero = false;}
279
}
280
if (mstrReturn.Substring(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) {mstrReturn = mstrReturn.Substring(1);}
281
if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "0"){mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}
282
if (mstrReturn.Substring(0, 1) == "0") {mstrReturn = mstrReturn.Substring(1);}
283
if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "T") {mstrReturn = mstrReturn + "Y";}
284
return mstrReturn;
285
}
286
287
288
}
289
}
using System;2

3
namespace TestUpperRMB4
{5
/// <summary>6
/// 本类实现阿拉伯数字到大写中文的转换7
/// 该类没有对非法数字进行判别,请事先自己判断数字是否合法8
/// </summary>9
public class ChineseNum10
{11
public static string GetChineseNum(string p_num)12
{13
ChineseNum cn = new ChineseNum();14
15
return cn.NumToChn(p_num);16
}17

18
public static string GetUpperMoney(double p_Money)19
{20
ChineseNum cn = new ChineseNum();21

22
return cn.GetMoneyChinese(p_Money);23
}24

25
//转换数字26
private char CharToNum(char x)27
{28
string stringChnNames="零一二三四五六七八九";29
string stringNumNames="0123456789";30
return stringChnNames[stringNumNames.IndexOf(x)];31
}32

33
//转换万以下整数34
private string WanStrToInt(string x)35
{36
string[] stringArrayLevelNames=new string[4] {"","十","百","千"};37
string ret="";38
int i;39
for (i=x.Length-1;i>=0;i--)40
if (x[i] == '0')41
{42
ret = CharToNum(x[i]) + ret;43
}44
else45
{46
ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;47
}48
while ((i = ret.IndexOf("零零")) != -1)49
{50
ret = ret.Remove(i, 1);51
}52
if (ret[ret.Length - 1] == '零' && ret.Length > 1)53
{54
ret = ret.Remove(ret.Length - 1, 1);55
}56
if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")57
{58
ret = ret.Remove(0, 1);59
}60
return ret;61
}62
//转换整数63
private string StrToInt(string x)64
{65
int len=x.Length;66
string ret,temp;67
if (len <= 4)68
{69
ret = WanStrToInt(x);70
}71
else if (len <= 8)72
{73
ret = WanStrToInt(x.Substring(0, len - 4)) + "万";74
temp = WanStrToInt(x.Substring(len - 4, 4));75
if (temp.IndexOf("千") == -1 && temp != "")76
ret += "零" + temp;77
else78
ret += temp;79
}80
else81
{82
ret = WanStrToInt(x.Substring(0, len - 8)) + "亿";83
temp = WanStrToInt(x.Substring(len - 8, 4));84
if (temp.IndexOf("千") == -1 && temp != "")85
{86
ret += "零" + temp;87
}88
else89
{90
ret += temp;91
}92
ret += "万";93
temp = WanStrToInt(x.Substring(len - 4, 4));94
if (temp.IndexOf("千") == -1 && temp != "")95
{96
ret += "零" + temp;97
}98
else99
{100
ret += temp;101
}102

103
}104
int i;105
if ((i = ret.IndexOf("零万")) != -1)106
{107
ret = ret.Remove(i + 1, 1);108
}109
while ((i = ret.IndexOf("零零")) != -1)110
{111
ret = ret.Remove(i, 1);112
}113
if (ret[ret.Length - 1] == '零' && ret.Length > 1)114
{115
ret = ret.Remove(ret.Length - 1, 1);116
}117
return ret;118
}119
//转换小数120
private string StrToDouble(string x)121
{122
string ret="";123
for (int i = 0; i < x.Length; i++)124
{125
ret += CharToNum(x[i]);126
}127
return ret;128
}129

130
private string NumToChn(string x)131
{132
if (x.Length == 0)133
{134
return "";135
}136
string ret="";137
if (x[0]=='-')138
{139
ret="负";140
x=x.Remove(0,1);141
}142
if (x[0].ToString() == ".")143
{144
x = "0" + x;145
}146
if (x[x.Length - 1].ToString() == ".")147
{148
x = x.Remove(x.Length - 1, 1);149
}150
if (x.IndexOf(".") > -1)151
{152
ret += StrToInt(x.Substring(0, x.IndexOf("."))) + "点" + StrToDouble(x.Substring(x.IndexOf(".") + 1));153
}154
else155
{156
ret += StrToInt(x);157
}158
return ret;159
}160

161

162
private string GetMoneyChinese(Double Money) 163
{164
int i;165
string mstrSource;166
167
if (Money == 0)168
{169
return "";170
}171
mstrSource = Money.ToString("#0.00");172
i = mstrSource.IndexOf(".");173
if (i > 0) {mstrSource = mstrSource.Replace(".","");}174
if (mstrSource.Substring(0,1) == "0") {mstrSource = mstrSource.Remove(0,1);}175
176
mstrSource = NumstrToChinese(mstrSource);177
if (mstrSource.Length == 0) {return "";}178
//负179
if (Money < 0) 180
{181
mstrSource = "负" + mstrSource;182
} 183
184
mstrSource=mstrSource.Replace("0","零");185
mstrSource=mstrSource.Replace("1","壹");186
mstrSource=mstrSource.Replace("2","贰");187
mstrSource=mstrSource.Replace("3","叁");188
mstrSource=mstrSource.Replace("4","肆");189
mstrSource=mstrSource.Replace("5","伍");190
mstrSource=mstrSource.Replace("6","陆");191
mstrSource=mstrSource.Replace("7","柒");192
mstrSource=mstrSource.Replace("8","捌");193
mstrSource=mstrSource.Replace("9","玖");194
mstrSource=mstrSource.Replace("M","亿");195
mstrSource=mstrSource.Replace("W","万");196
mstrSource=mstrSource.Replace("S","仟");197
mstrSource=mstrSource.Replace("H","佰");198
mstrSource=mstrSource.Replace("T","拾");199
mstrSource=mstrSource.Replace("Y","圆");200
mstrSource=mstrSource.Replace("J","角");201
mstrSource=mstrSource.Replace("F","分");202
if (mstrSource.Substring(mstrSource.Length-1, 1) != "分") 203
{204
mstrSource = mstrSource + "整";205
}206
return mstrSource;207
}208

209
//金额转换210
private string NumstrToChinese(string numstr) 211
{212
int i;213
int j;214
string mstrChar;215
string[] mstrFlag=new string[4];216
string mstrReturn="";217
bool mblnAddzero=false;218

219
mstrFlag[0] = "";220
mstrFlag[1] = "T";221
mstrFlag[2] = "H";222
mstrFlag[3] = "S";223
224
for (i = 1;i<=numstr.Length;i++) 225
{226
j = numstr.Length - i;227
mstrChar = numstr.Substring(i-1,1); 228
if (mstrChar != "0" && j > 1) {mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2) % 4];}229
if (mstrChar == "0" && mblnAddzero==false)230
{231
mstrReturn = mstrReturn + "0";232
mblnAddzero = true;233
}234
if (j == 14)235
{236
if (mstrReturn.Substring(mstrReturn.Length-1) == "0") 237
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}238
else239
{mstrReturn = mstrReturn + "W";}240
}241
if (j == 2) 242
{243
if (mstrReturn.Substring(mstrReturn.Length-1,1) == "0")244
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "Y0";}245
else246
{mstrReturn = mstrReturn + "Y";}247
//元248
}249
if (j == 6)250
{251
if (mstrReturn.Length > 2) 252
{253
if (mstrReturn.Substring(mstrReturn.Length-2) != "M0") 254
{255
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")256
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}257
else258
{mstrReturn = mstrReturn + "W";}259
}260
}261
else262
{263
if (mstrReturn.Substring(mstrReturn.Length-1) == "0")264
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}265
else266
{mstrReturn = mstrReturn + "W";}267
}268
}269
if (j == 10)270
{271
if (mstrReturn.Substring(mstrReturn.Length-1) == "0") 272
{mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "M0";}273
else274
{mstrReturn = mstrReturn + "M";}275
}276
if (j == 0 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "F";}277
if (j == 1 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "J";}278
if (mstrChar != "0") {mblnAddzero = false;}279
}280
if (mstrReturn.Substring(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) {mstrReturn = mstrReturn.Substring(1);}281
if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "0"){mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}282
if (mstrReturn.Substring(0, 1) == "0") {mstrReturn = mstrReturn.Substring(1);}283
if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "T") {mstrReturn = mstrReturn + "Y";}284
return mstrReturn;285
}286

287

288
}289
}这是我来博客园写的处女作^_^~~



浙公网安备 33010602011771号