提交 | 用户 | age
|
a6a76f
|
1 |
import java.util.Map; |
F |
2 |
|
|
3 |
import net.sf.jasperreports.engine.JRDefaultScriptlet; |
|
4 |
import net.sf.jasperreports.engine.JRScriptletException; |
|
5 |
import net.sf.jasperreports.engine.fill.JRFillVariable; |
|
6 |
|
|
7 |
/** |
|
8 |
* 大写金额转换Scriptlet类 |
|
9 |
* |
|
10 |
* @author |
|
11 |
*/ |
|
12 |
public class TransChineseMoneyScriptlet extends JRDefaultScriptlet { |
|
13 |
/**//* |
|
14 |
* 默认构造方法 |
|
15 |
*/ |
|
16 |
public TransChineseMoneyScriptlet() { |
|
17 |
|
|
18 |
} |
|
19 |
|
|
20 |
/** |
|
21 |
* 获得金额的汉字大写格式 <br> |
|
22 |
* |
|
23 |
* @param money |
|
24 |
* 小写金额字符串 |
|
25 |
* @return 大写的汉字金额 |
|
26 |
*/ |
|
27 |
public static String getChineseMoney(String money) { |
|
28 |
String text = toUpperMoneyNum(money); |
|
29 |
if (text.substring(text.length() - 1).equals("元")) { |
|
30 |
text += "整"; |
|
31 |
} |
|
32 |
return text; |
|
33 |
} |
|
34 |
public static String getChineseDigit(String num) { |
|
35 |
String text = toUpperNum(num); |
|
36 |
return text; |
|
37 |
} |
|
38 |
static boolean isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假 |
|
39 |
static boolean isZero = false;// 千位上开始为零,为真.防止千位开始连续5为以上为0 |
|
40 |
|
|
41 |
|
|
42 |
public static String toUpperNum(String numStr) { |
|
43 |
if (!isNum(numStr)) { |
|
44 |
return ""; |
|
45 |
} |
|
46 |
isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假 |
|
47 |
isZero = false;// 这两个全局变量必须还原,因为循环调用此方法是, |
|
48 |
// 转换前一个数所做的值得改变还会保留,可能进此方法,两个判断词就true了 |
|
49 |
String retNum = ""; |
|
50 |
int num1 = 0; |
|
51 |
int zeroNo = 0; |
|
52 |
for (int i = numStr.length() - 1; i > 0; i--) {// 去除最后为零的数字 |
|
53 |
if(numStr.charAt(i)=='.')break; |
|
54 |
num1 = Integer.parseInt(String.valueOf(numStr.charAt(i))); |
|
55 |
if (num1 == 0) { |
|
56 |
zeroNo++; |
|
57 |
} else { |
|
58 |
break; |
|
59 |
} |
|
60 |
} |
|
61 |
numStr = numStr.substring(0, numStr.length() - zeroNo); |
|
62 |
String toPoint = numStr; |
|
63 |
String toPointEnd = ""; |
|
64 |
if (numStr.indexOf(".") != -1) { |
|
65 |
toPoint = numStr.substring(0, numStr.indexOf(".")); |
|
66 |
toPointEnd = numStr.substring(numStr.indexOf(".") + 1); |
|
67 |
} |
|
68 |
// numStr |
|
69 |
int len = toPoint.length(); |
|
70 |
toPoint += toPointEnd; |
|
71 |
boolean oneZero = false;// 去除如“00234”前面的零,判断数字中的零是否从第一个连续开始都为零 |
|
72 |
for (int i = 0; i < toPoint.length(); i++) { |
|
73 |
num1 = Integer.parseInt(String.valueOf(toPoint.charAt(i))); |
|
74 |
if (num1 == 0) { |
|
75 |
if (i == 0) { |
|
76 |
oneZero = true;// 第一个数字为零 |
|
77 |
continue; |
|
78 |
} |
|
79 |
if (oneZero) {// 从第一个为连续零 |
|
80 |
continue; |
|
81 |
} |
|
82 |
} else { |
|
83 |
oneZero = false;// 之后不为连续 |
|
84 |
} |
|
85 |
retNum += getNumDigOnSite(num1, len - i); |
|
86 |
} |
|
87 |
return (retNum.equals("")) ? "零" : retNum; |
|
88 |
} |
|
89 |
/** |
|
90 |
* 将数字转成大写,为字符串形式的数字 |
|
91 |
* |
|
92 |
* @param numStr |
|
93 |
* 字符串,当可转成数字 |
|
94 |
* @return 大写数字的字符串 |
|
95 |
*/ |
|
96 |
public static String toUpperMoneyNum(String numStr) { |
|
97 |
if (!isNum(numStr)) { |
|
98 |
return ""; |
|
99 |
} |
|
100 |
isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假 |
|
101 |
isZero = false;// 这两个全局变量必须还原,因为循环调用此方法是, |
|
102 |
// 转换前一个数所做的值得改变还会保留,可能进此方法,两个判断词就true了 |
|
103 |
String retNum = ""; |
|
104 |
int num1 = 0; |
|
105 |
int zeroNo = 0; |
|
106 |
for (int i = numStr.length() - 1; i > 0; i--) {// 去除最后为零的数字 |
|
107 |
if(numStr.charAt(i)=='.')break; |
|
108 |
num1 = Integer.parseInt(String.valueOf(numStr.charAt(i))); |
|
109 |
if (num1 == 0) { |
|
110 |
zeroNo++; |
|
111 |
} else { |
|
112 |
break; |
|
113 |
} |
|
114 |
} |
|
115 |
numStr = numStr.substring(0, numStr.length() - zeroNo); |
|
116 |
String toPoint = numStr; |
|
117 |
String toPointEnd = ""; |
|
118 |
if (numStr.indexOf(".") != -1) { |
|
119 |
toPoint = numStr.substring(0, numStr.indexOf(".")); |
|
120 |
toPointEnd = numStr.substring(numStr.indexOf(".") + 1); |
|
121 |
} |
|
122 |
// numStr |
|
123 |
int len = toPoint.length(); |
|
124 |
toPoint += toPointEnd; |
|
125 |
boolean oneZero = false;// 去除如“00234”前面的零,判断数字中的零是否从第一个连续开始都为零 |
|
126 |
for (int i = 0; i < toPoint.length(); i++) { |
|
127 |
num1 = Integer.parseInt(String.valueOf(toPoint.charAt(i))); |
|
128 |
if (num1 == 0) { |
|
129 |
if (i == 0) { |
|
130 |
oneZero = true;// 第一个数字为零 |
|
131 |
continue; |
|
132 |
} |
|
133 |
if (oneZero) {// 从第一个为连续零 |
|
134 |
continue; |
|
135 |
} |
|
136 |
} else { |
|
137 |
oneZero = false;// 之后不为连续 |
|
138 |
} |
|
139 |
retNum += getNumOnSite(num1, len - i); |
|
140 |
} |
|
141 |
return (retNum.equals("")) ? "零元" : retNum; |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* 判断此字符能否转成数字double类型 |
|
146 |
* |
|
147 |
* @param num |
|
148 |
* 字符串数字 |
|
149 |
* @return 是数字为True |
|
150 |
*/ |
|
151 |
public static boolean isNum(String num) { |
|
152 |
try { |
|
153 |
double num1 = Double.parseDouble(num); |
|
154 |
num1++;// 让其计算下,没错就一定数数字 |
|
155 |
} catch (Exception e) { |
|
156 |
// 只做捕获,不让程序死掉就行了 |
|
157 |
return false; |
|
158 |
} |
|
159 |
return true; |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* 数num在位置site上怎么读 |
|
164 |
* |
|
165 |
* @param num |
|
166 |
* 数 |
|
167 |
* @param site |
|
168 |
* 位数 |
|
169 |
* @return String 读法 |
|
170 |
*/ |
|
171 |
private static String getNumOnSite(int num, int site) { |
|
172 |
String str = ""; |
|
173 |
if (num == 0) { |
|
174 |
isZeroNum = true;// 记录数字为零 |
|
175 |
if (site % 4 == 0) { |
|
176 |
isZero = true;// 记录千为为零 |
|
177 |
} |
|
178 |
if (site % 4 == 1) { |
|
179 |
// 保险起见 |
|
180 |
if (!isZero) { |
|
181 |
str = getSite(site);// 给了单位,就就不需零什么了,如四十万,零即清掉 |
|
182 |
} |
|
183 |
isZeroNum = false;// 等于把前面记录有零的情况清掉 |
|
184 |
} |
|
185 |
if (site < -2) { |
|
186 |
str = getNum(num); |
|
187 |
}// 厘单位后的数字 |
|
188 |
return str; |
|
189 |
} |
|
190 |
if (isZeroNum) { |
|
191 |
str += getNum(0); |
|
192 |
isZeroNum = false; |
|
193 |
isZero = false;// 还原为假, |
|
194 |
} |
|
195 |
str += getNum(num) + getSite(site); |
|
196 |
return str; |
|
197 |
} |
|
198 |
private static String getNumDigOnSite(int num, int site) { |
|
199 |
String str = ""; |
|
200 |
if(site==0){ |
|
201 |
str += "点"; |
|
202 |
} |
|
203 |
if (num == 0) { |
|
204 |
isZeroNum = true;// 记录数字为零 |
|
205 |
if (site % 4 == 0) { |
|
206 |
isZero = true;// 记录千为为零 |
|
207 |
} |
|
208 |
if (site % 4 == 1) { |
|
209 |
// 保险起见 |
|
210 |
if (!isZero) { |
|
211 |
str = getSiteDig(site);// 给了单位,就就不需零什么了,如四十万,零即清掉 |
|
212 |
} |
|
213 |
isZeroNum = false;// 等于把前面记录有零的情况清掉 |
|
214 |
} |
|
215 |
if (site < -2) { |
|
216 |
str += getNum(num); |
|
217 |
}// 厘单位后的数字 |
|
218 |
return str; |
|
219 |
} |
|
220 |
if (isZeroNum) { |
|
221 |
str += getNum(0); |
|
222 |
isZeroNum = false; |
|
223 |
isZero = false;// 还原为假, |
|
224 |
} |
|
225 |
str += getNum(num) + getSiteDig(site); |
|
226 |
return str; |
|
227 |
} |
|
228 |
/** |
|
229 |
* 得到数字对应的大写字 |
|
230 |
* |
|
231 |
* @param num |
|
232 |
* 数字(0-9) |
|
233 |
* @return String |
|
234 |
*/ |
|
235 |
private static String getNum(int num) { |
|
236 |
if (num > 9) { |
|
237 |
return ""; |
|
238 |
} |
|
239 |
String str = "零壹贰叁肆伍陆柒捌玖"; |
|
240 |
return str.substring(num, num + 1); |
|
241 |
} |
|
242 |
private static String getSiteDig(int site){ |
|
243 |
String str = ""; |
|
244 |
if (site < 2) { |
|
245 |
str = ""; |
|
246 |
// switch (site) { |
|
247 |
// case -2: |
|
248 |
// //str = ""; |
|
249 |
// break; |
|
250 |
// case -1: |
|
251 |
// //str = ""; |
|
252 |
// break;// 这上面写出小数点后的单位情况 |
|
253 |
// } |
|
254 |
return str; |
|
255 |
} |
|
256 |
String siteStr="仟佰拾兆仟佰拾亿仟佰拾万仟佰拾"; |
|
257 |
if(site>siteStr.length()){ |
|
258 |
return "单位不知"; |
|
259 |
} |
|
260 |
return String.valueOf(siteStr.charAt(siteStr.length()-site+1)); |
|
261 |
} |
|
262 |
/** |
|
263 |
* 得到位置所对应的单位 |
|
264 |
* |
|
265 |
* @param site |
|
266 |
* 位置数小数点前的 |
|
267 |
* @return String |
|
268 |
*/ |
|
269 |
private static String getSite(int site) { |
|
270 |
String str = ""; |
|
271 |
if (site < 0) { |
|
272 |
str = ""; |
|
273 |
switch (site) { |
|
274 |
case -2: |
|
275 |
str = "厘"; |
|
276 |
break; |
|
277 |
case -1: |
|
278 |
str = "分"; |
|
279 |
break;// 这上面写出小数点后的单位情况 |
|
280 |
} |
|
281 |
return str; |
|
282 |
} |
|
283 |
int yu = site % 4; |
|
284 |
int chu = site / 4; |
|
285 |
switch (yu) { |
|
286 |
case 1: |
|
287 |
switch (chu) { |
|
288 |
case 0: |
|
289 |
str = "元"; |
|
290 |
break; |
|
291 |
case 1: |
|
292 |
str = "万"; |
|
293 |
break; |
|
294 |
case 2: |
|
295 |
str = "亿"; |
|
296 |
break; |
|
297 |
case 3: |
|
298 |
str = "兆"; |
|
299 |
break; // 这里写上每递进的4位的单位 |
|
300 |
} |
|
301 |
break; |
|
302 |
case 2: |
|
303 |
str = "拾"; |
|
304 |
break; |
|
305 |
case 3: |
|
306 |
str = "佰"; |
|
307 |
break; |
|
308 |
case 0: |
|
309 |
switch (chu) { |
|
310 |
case 0: |
|
311 |
str = "角"; |
|
312 |
break; |
|
313 |
default: |
|
314 |
str = "仟";// 其他除4余零都是千了 |
|
315 |
break; |
|
316 |
} |
|
317 |
} |
|
318 |
if (isZeroNum) { |
|
319 |
if (site % 4 != 1 || isZero) {// 如200000345的千字位千万为零,却读到万时不加万,即""; |
|
320 |
str = ""; |
|
321 |
} |
|
322 |
} // 处理中间的零 |
|
323 |
return str; |
|
324 |
} |
|
325 |
|
|
326 |
private void getLowerToUpper() throws JRScriptletException { |
|
327 |
String keyString = ""; |
|
328 |
Double lowerMoney = 0.0; |
|
329 |
String upperMoney =""; |
|
330 |
String[] lowerStr = null; |
|
331 |
for (Object obj : variablesMap.keySet()) { |
|
332 |
keyString = String.valueOf(obj).trim(); |
|
333 |
lowerStr = keyString.split("_"); |
|
334 |
if (lowerStr[0].equals("lowerMoney")) { |
|
335 |
lowerMoney = getVariableValue("lowerMoney_" + lowerStr[1]) == null ? new Double( |
|
336 |
0.0) |
|
337 |
: java.lang.Double.parseDouble(String.valueOf(getVariableValue("lowerMoney_" |
|
338 |
+ lowerStr[1]))); |
|
339 |
upperMoney = getChineseMoney(lowerMoney + ""); |
|
340 |
if (hasPara("upperMoney_" + lowerStr[1],variablesMap)) |
|
341 |
this.setVariableValue("upperMoney_" + lowerStr[1], |
|
342 |
upperMoney); |
|
343 |
}else if(lowerStr[0].equals("lowerDigit")) { |
|
344 |
lowerMoney = getVariableValue("lowerDigit_" + lowerStr[1]) == null ? new Double( |
|
345 |
0.0) |
|
346 |
: (java.lang.Double) getVariableValue("lowerDigit_" |
|
347 |
+ lowerStr[1]); |
|
348 |
upperMoney = getChineseDigit(lowerMoney + ""); |
|
349 |
if (hasPara("upperDigit_" + lowerStr[1],variablesMap)) |
|
350 |
this.setVariableValue("upperDigit_" + lowerStr[1], |
|
351 |
upperMoney); |
|
352 |
} |
|
353 |
} |
|
354 |
} |
|
355 |
|
|
356 |
public static boolean hasPara(String para,Map<String, JRFillVariable> vMap){//Map<String, Object> |
|
357 |
for (Object obj:vMap.keySet()) { |
|
358 |
if(para.equals(obj.toString())) return true; |
|
359 |
} |
|
360 |
return false; |
|
361 |
} |
|
362 |
/** |
|
363 |
* Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't |
|
364 |
* modify or move please! |
|
365 |
*/ |
|
366 |
public void afterColumnInit() throws JRScriptletException { |
|
367 |
// getLowerToUpper(); |
|
368 |
super.afterColumnInit(); |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't |
|
373 |
* modify or move please! |
|
374 |
*/ |
|
375 |
/** |
|
376 |
* Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't |
|
377 |
* modify or move please! |
|
378 |
*/ |
|
379 |
public void afterDetailEval() throws JRScriptletException { |
|
380 |
getLowerToUpper(); |
|
381 |
super.afterDetailEval(); |
|
382 |
} |
|
383 |
|
|
384 |
/** |
|
385 |
* End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't |
|
386 |
* modify or move please! |
|
387 |
*/ |
|
388 |
/** |
|
389 |
* Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't |
|
390 |
* modify or move please! |
|
391 |
*/ |
|
392 |
public void afterGroupInit(String groupName) throws JRScriptletException { |
|
393 |
// System.out.println(groupName); |
|
394 |
super.afterGroupInit(groupName); |
|
395 |
} |
|
396 |
|
|
397 |
/** |
|
398 |
* End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't |
|
399 |
* modify or move please! |
|
400 |
*/ |
|
401 |
/** |
|
402 |
* Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't |
|
403 |
* modify or move please! |
|
404 |
*/ |
|
405 |
public void afterPageInit() throws JRScriptletException { |
|
406 |
// getLowerToUpper(); |
|
407 |
super.afterPageInit(); |
|
408 |
} |
|
409 |
|
|
410 |
/** |
|
411 |
* End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify |
|
412 |
* or move please! |
|
413 |
*/ |
|
414 |
/** |
|
415 |
* Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't |
|
416 |
* modify or move please! |
|
417 |
*/ |
|
418 |
public void afterReportInit() throws JRScriptletException { |
|
419 |
// getLowerToUpper(); |
|
420 |
super.afterReportInit(); |
|
421 |
} |
|
422 |
|
|
423 |
/** |
|
424 |
* End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't |
|
425 |
* modify or move please! |
|
426 |
*/ |
|
427 |
/** |
|
428 |
* Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't |
|
429 |
* modify or move please! |
|
430 |
*/ |
|
431 |
public void beforeColumnInit() throws JRScriptletException { |
|
432 |
// getLowerToUpper(); |
|
433 |
} |
|
434 |
|
|
435 |
/** |
|
436 |
* End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't |
|
437 |
* modify or move please! |
|
438 |
*/ |
|
439 |
/** |
|
440 |
* Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't |
|
441 |
* modify or move please! |
|
442 |
*/ |
|
443 |
public void beforeDetailEval() throws JRScriptletException { |
|
444 |
// getLowerToUpper(); |
|
445 |
} |
|
446 |
|
|
447 |
/** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment */ |
|
448 |
|
|
449 |
/** |
|
450 |
* End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't |
|
451 |
* modify or move please! |
|
452 |
*/ |
|
453 |
/** |
|
454 |
* Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't |
|
455 |
* modify or move please! |
|
456 |
*/ |
|
457 |
public void beforeGroupInit(String groupName) throws JRScriptletException { |
|
458 |
|
|
459 |
} |
|
460 |
|
|
461 |
/** |
|
462 |
* End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't |
|
463 |
* modify or move please! |
|
464 |
*/ |
|
465 |
/** |
|
466 |
* Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't |
|
467 |
* modify or move please! |
|
468 |
*/ |
|
469 |
public void beforePageInit() throws JRScriptletException { |
|
470 |
// getLowerToUpper(); |
|
471 |
} |
|
472 |
|
|
473 |
/** |
|
474 |
* End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't |
|
475 |
* modify or move please! |
|
476 |
*/ |
|
477 |
/** |
|
478 |
* Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't |
|
479 |
* modify or move please! |
|
480 |
*/ |
|
481 |
public void beforeReportInit() throws JRScriptletException { |
|
482 |
// getLowerToUpper(); |
|
483 |
} |
|
484 |
|
|
485 |
public static void main(String[] args) { |
|
486 |
System.out.println(getSiteDig(17)); |
|
487 |
System.out.println(toUpperNum("300304.11")); |
|
488 |
System.out.println(toUpperMoneyNum("300304.11")); |
|
489 |
} |
|
490 |
} |