今天写了一个C#版本的大小写转换类,想着也要更新一下我的日记了,便把它传了上来。 哪位路过的高手,请指点。
这个类提供了一个静态方法叫做 Convert ( double ),传入一个9千万亿以内的数值它都能正确的转换为中文的大写。基本的思路是将传入的数据分为整数部分和小数部分分别来处理,小数部分的处理比较简单,整数部分的处理我作了相应的注释。
1
using System;
2
3
namespace Tools{
4
/// <summary>
5
/// DigitalToChinese :实现将人民币的数字形式转换为中文格式。
6
/// </summary>
7
public class DigitalToChinese
8
{
9
public DigitalToChinese(){
10
}
11
12
public static string Convert ( double digital ){
13
if ( digital == 0.00f )
14
return "零元整";
15
16
string buf = ""; /* 存放返回结果 */
17
string strDecPart = ""; /* 存放小数部分的处理结果 */
18
string strIntPart = ""; /* 存放整数部分的处理结果 */
19
20
/* 将数据分为整数和小数部分 */
21
char [] cDelim = {'.'};
22
string [] tmp = null;
23
string strDigital = digital.ToString ();
24
if ( strDigital[0] =='.'){ /* 处理数据首位为小数点的情况 */
25
strDigital = "0" + strDigital;
26
}
27
tmp = strDigital.Split ( cDelim,2);
28
29
/* 整数部分的处理 */
30
if ( tmp[0].Length > 15 ) {
31
throw new Exception ("数值超出处理范围。");
32
}
33
bool flag = true; /* 标示整数部分是否为零 */
34
if ( digital >= 1.00f ) {
35
strIntPart = ConvertInt( tmp[0] );
36
flag = false;
37
}
38
39
/* 存在小数部分,则处理小数部分 */
40
if ( tmp.Length == 2 )
41
strDecPart = ConvertDecimal ( tmp[1],flag);
42
else
43
strDecPart = "整";
44
45
buf = strIntPart + strDecPart;
46
return buf;
47
}
48
49
50
51
private static string ConvertInt ( string intPart ) {
52
string buf = "";
53
int length = intPart.Length ;
54
int curUnit = length;
55
56
/* 处理除个位以上的数据 */
57
string tmpValue = ""; /* 记录当前数值的中文形式 */
58
string tmpUnit = ""; /* 记录当前数值对应的中文单位 */
59
int i ;
60
for ( i = 0 ; i < length - 1 ; i ++ ,curUnit --){
61
if ( intPart [ i ] != '0' ){
62
tmpValue = DigToCC ( intPart [ i ] );
63
tmpUnit = GetUnit ( curUnit -1 );
64
}
65
else {
66
/* 如果当前的单位是"万、亿",则需要把它记录下来 */
67
if ( (curUnit -1 ) % 4 == 0 ) {
68
tmpValue = "";
69
tmpUnit = GetUnit ( curUnit -1 );
70
}
71
else {
72
tmpUnit = "";
73
/* 如果当前位是零,则需要判断它的下一位是否为零,再确定是否记录'零' */
74
if ( intPart [i+1] != '0' ){
75
tmpValue = "零";
76
}
77
else {
78
tmpValue = "";
79
}
80
}
81
}
82
buf += tmpValue + tmpUnit;
83
}
84
85
/* 处理个位数据 */
86
if ( intPart [i] != '0' )
87
buf += DigToCC ( intPart [i] );
88
buf += "元";
89
90
return buf;
91
}
92
93
94
/// <summary>
95
/// 小数部分的处理
96
/// </summary>
97
/// <param name="decPart">需要处理的小数部分</param>
98
/// <param name="flag">标示整数部分是否为零</param>
99
/// <returns></returns>
100
private static string ConvertDecimal ( string decPart,bool flag ) {
101
string buf = "";
102
if ( ( decPart [0] == '0') && (decPart.Length > 1 )){
103
if ( flag == false ) {
104
buf += "零";
105
}
106
if ( decPart[1] != '0'){
107
buf += DigToCC( decPart[1] )+"分";
108
}
109
}
110
else{
111
buf += DigToCC ( decPart[0] ) +"角";
112
if ( ( decPart.Length >1) && (decPart [1] != '0' ) ){
113
buf += DigToCC( decPart[1] )+"分";
114
}
115
}
116
buf += "整";
117
return buf;
118
}
119
120
121
/// <summary>
122
/// 获取人民币中文形式的对应位置的单位标志
123
/// </summary>
124
/// <param name="n"></param>
125
/// <returns></returns>
126
private static string GetUnit( int n ) {
127
switch( n ) {
128
case 1:
129
return "拾";
130
case 2:
131
return "佰";
132
case 3:
133
return "仟";
134
case 4:
135
return "万";
136
case 5:
137
return "拾";
138
case 6:
139
return "佰";
140
case 7:
141
return "仟";
142
case 8:
143
return "亿";
144
case 9:
145
return "拾";
146
case 10:
147
return "佰";
148
case 11:
149
return "仟";
150
case 12:
151
return "万";
152
case 13:
153
return "拾";
154
case 14:
155
return "佰";
156
case 15:
157
return "仟";
158
default:
159
return " ";
160
}
161
}
162
163
164
165
/// <summary>
166
/// 数字转换为相应的中文字符 ( Digital To Chinese Char )
167
/// </summary>
168
/// <param name="c">以字符形式存储的数字</param>
169
/// <returns></returns>
170
private static string DigToCC(char c) {
171
switch( c ) {
172
case '1':
173
return "壹";
174
case '2':
175
return "贰";
176
case '3':
177
return "叁";
178
case '4':
179
return "肆";
180
case '5':
181
return "伍";
182
case '6':
183
return "陆";
184
case '7':
185
return "柒";
186
case '8':
187
return "捌";
188
case '9':
189
return "玖";
190
case '0':
191
return "零";
192
default:
193
return " ";
194
}
195
}
196
}
197
}
198

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

下面是一个测试代码:
1
using System;
2
3
namespace Tools{
4
/// <summary>
5
/// Class1 的摘要说明。
6
/// </summary>
7
class Class1
8
{
9
/// <summary>
10
/// 应用程序的主入口点。
11
/// </summary>
12
[STAThread]
13
static void Main(string[] args)
14
{
15
string read = null;
16
Console.WriteLine ("输入数值,'q'推出!");
17
18
do{
19
20
read = Console.ReadLine ();
21
if ( read =="q" || read == "Q")
22
return;
23
try{
24
Console.WriteLine ( DigitalToChinese.Convert ( Convert.ToDouble ( read )));
25
}
26
catch ( Exception ){
27
Console.WriteLine ("输入的数据不符合要求,请输入正确的数值.");
28
}
29
}while ( 1 == 1 );
30
}
31
}
32
}
33

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33
