1
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
| /**
* 人民币小写转换成大写
* @param s 人民币小写(比如:12043000.01)
* @return 人民币大写(比如:壹仟贰佰零肆万叁仟元零壹分)
*/
public static String getNumberToRMB(String s) {
if (s == null || s.trim().length() == 0) {
return s;
}
StringBuilder rmbChinese = new StringBuilder();
// 小数点位置
int xsdwz = s.indexOf(".");
// 整数部分
String integral = "";
// 角分部分
String decimal = "";
String[] wy = {};
int xh = 0;
String prefix = ""; // 整数部分转化的结果
String suffix = ""; // 小数部分转化的结果
if (xsdwz >= 0) {
integral = s.substring(0, xsdwz);
if (!integral.equals("")) {
if (Double.parseDouble(integral) == 0) {
integral = "";
}
}
decimal = s.substring(xsdwz + 1, s.length());
//for(int i=0;i<new)
if (decimal.length() < 2) {
decimal += "0";
}
} else {
integral = s;
decimal = "";
//new2="00";
}
if (integral.length() > 0) {
if (integral.substring(0, 1).equals("-") || integral.substring(0, 1).equals("+")) {
if (integral.substring(0, 1).equals("-")) {
rmbChinese.append("负");
}
// 去掉前面的正、负符号
integral = integral.substring(1, integral.length());
}
}
if (integral.length() % 4 == 0) {
xh = integral.length() / 4;
//wy = new String[new1.length() / 4];
} else {
xh = integral.length() / 4 + 1;
//wy = new String[new1.length() / 4 + 1];
}
wy = new String[xh];
for (int i = 0; i < xh; i++) {
if ((i + 1) * 4 <= integral.length()) {
wy[i] = integral.substring(integral.length() - (i + 1) * 4, integral.length() - i * 4);
} else {
wy[i] = integral.substring(0, integral.length() % 4);
}
//System.out.println(wy);
}
if (integral.length() > 0 && Integer.parseInt(decimal) > 9) {
rmbChinese.append(getDxZs(integral, 1, wy));
rmbChinese.append(getDxJf(decimal));
} else if (integral.length() > 0 && (Integer.parseInt(decimal) > 0 && Integer.parseInt(decimal) <= 9)) {
rmbChinese.append(getDxZs(integral, 3, wy));
rmbChinese.append(getDxJf(decimal));
} else if (integral.length() > 0 && Integer.parseInt(decimal) == 0) {
rmbChinese.append(getDxZs(integral, 2, wy));
} else if (integral.length() == 0 && Integer.parseInt(decimal) > 0) {
rmbChinese.append(getDxJf(decimal));
}
return rmbChinese.toString();
}
/**
* 获得大写整数部分
* @param s
* @param i1
* @param wy
* @return
*/
private static String getDxZs(String s, int i1, String[] wy) {
StringBuilder dxzs = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (Integer.parseInt(s.substring(i, 1 + i)) > 0) {
if (i > 0 && i < s.length() - 1) {
if (Integer.parseInt(s.substring(i - 1, i)) == 0) {
dxzs.append("零");
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
} else {
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
}
} else if (i == s.length() - 1) {// && i!=0) {
if (i != 0 && Integer.parseInt(s.substring(i - 1, i)) == 0) {
dxzs.append("零");
}
if (i1 == 1) {
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
} else if (i1 == 2) {
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
dxzs.append("整");
} else if (i1 == 3) {
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
dxzs.append("零");
}
} else if (i == 0) { //&& i != s.length() - 1) {
dxzs.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxzs.append(getWeiShu(s.length() - i));
}
} else if (Integer.parseInt(s.substring(i, i + 1)) == 0) {
if (i > 0 && i < s.length() - 1) {
if (s.length() - i == 9) {
if (Integer.parseInt(wy[2]) != 0) {
dxzs.append("亿");
}
} else if (s.length() - i == 5) {
if (Integer.parseInt(wy[1]) != 0) {
dxzs.append("万");
}
}
} else if (i > 0 && i == s.length() - 1) {
if (i1 == 1) {
dxzs.append("元零");
} else if (i1 == 2) {
dxzs.append("元整");
} else if (i1 == 3) {
dxzs.append("元零");
}
}
}
}
return dxzs.toString();
}
/**
* 获得大写角分部分
* @param s
* @return
*/
private static String getDxJf(String s) {
StringBuilder dxjf = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (Integer.parseInt(s.substring(i, i + 1)) != 0) {
dxjf.append(getHanZi(Integer.parseInt(s.substring(i, i + 1))));
dxjf.append(getJiaFen(i + 1));
} else if (Integer.parseInt(s.substring(i, i + 1)) == 0 && i == 1) {
dxjf.append("整");
}
}
return dxjf.toString();
}
/**
* 获得角、分
* @param s
* @return
*/
private static String getJiaFen(int s) {
String hz = "";
if (s == 1) {
hz = "角";
} else if (s == 2) {
hz = "分";
}
return hz;
}
/**
* 获得数字的大写汉字
* @param s 0~9的整数
* @return 零~玖的汉字
*/
private static String getHanZi(int s) {
String[] hzDigit = {
"零",
"壹",
"贰",
"叁",
"肆",
"伍",
"陆",
"柒",
"捌",
"玖"
};
return hzDigit[s];
}
/**
* 获得位数
* @param s
* @return
*/
private static String getWeiShu(int s) {
String hz = "";
if (s == 1) {
hz = "元";
} else if (s == 2) {
hz = "拾";
} else if (s == 3) {
hz = "佰";
} else if (s == 4) {
hz = "仟";
} else if (s == 5) {
hz = "万";
} else if (s == 6) {
hz = "拾";
} else if (s == 7) {
hz = "佰";
} else if (s == 8) {
hz = "仟";
} else if (s == 9) {
hz = "亿";
} else if (s == 10) {
hz = "拾";
} else if (s == 11) {
hz = "佰";
} else if (s == 12) {
hz = "仟";
}
return hz;
} |