fs-danaus
2023-07-10 4849078e3450b8d3b3030a658a34dd58b0630fc5
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import java.util.Map;
 
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.fill.JRFillVariable;
 
/**
 * 大写金额转换Scriptlet类
 * 
 * @author
 */
public class TransChineseMoneyScriptlet extends JRDefaultScriptlet {
    /**//*
      * 默认构造方法
      */
    public TransChineseMoneyScriptlet() {
 
    }
 
    /**
     * 获得金额的汉字大写格式 <br>
     * 
     * @param money
     *            小写金额字符串
     * @return 大写的汉字金额
     */
    public static String getChineseMoney(String money) {
    String text = toUpperMoneyNum(money);
    if (text.substring(text.length() - 1).equals("元")) {
        text += "整";
    }
    return text;
    }
    public static String getChineseDigit(String num) {
    String text = toUpperNum(num);    
    return text;
    }
    static boolean isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假
    static boolean isZero = false;// 千位上开始为零,为真.防止千位开始连续5为以上为0
 
  
    public static String toUpperNum(String numStr) {
    if (!isNum(numStr)) {
        return "";
    }
    isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假
    isZero = false;// 这两个全局变量必须还原,因为循环调用此方法是,
    // 转换前一个数所做的值得改变还会保留,可能进此方法,两个判断词就true了
    String retNum = "";
    int num1 = 0;
    int zeroNo = 0;
    for (int i = numStr.length() - 1; i > 0; i--) {// 去除最后为零的数字
        if(numStr.charAt(i)=='.')break;
        num1 = Integer.parseInt(String.valueOf(numStr.charAt(i)));
        if (num1 == 0) {
        zeroNo++;
        } else {
        break;
        }
    }
    numStr = numStr.substring(0, numStr.length() - zeroNo);
    String toPoint = numStr;
    String toPointEnd = "";
    if (numStr.indexOf(".") != -1) {
        toPoint = numStr.substring(0, numStr.indexOf("."));
        toPointEnd = numStr.substring(numStr.indexOf(".") + 1);
    }
    // numStr
    int len = toPoint.length();
    toPoint += toPointEnd;
    boolean oneZero = false;// 去除如“00234”前面的零,判断数字中的零是否从第一个连续开始都为零
    for (int i = 0; i < toPoint.length(); i++) {
        num1 = Integer.parseInt(String.valueOf(toPoint.charAt(i)));
        if (num1 == 0) {
        if (i == 0) {
            oneZero = true;// 第一个数字为零
            continue;
        }
        if (oneZero) {// 从第一个为连续零
            continue;
        }
        } else {
        oneZero = false;// 之后不为连续
        }
        retNum += getNumDigOnSite(num1, len - i);
    }
    return (retNum.equals("")) ? "零" : retNum;
    }
    /**
     * 将数字转成大写,为字符串形式的数字
     * 
     * @param numStr
     *            字符串,当可转成数字
     * @return 大写数字的字符串
     */
    public static String toUpperMoneyNum(String numStr) {
    if (!isNum(numStr)) {
        return "";
    }
    isZeroNum = false;// 数字为零,为全局变量出方法前必须还原为假
    isZero = false;// 这两个全局变量必须还原,因为循环调用此方法是,
    // 转换前一个数所做的值得改变还会保留,可能进此方法,两个判断词就true了
    String retNum = "";
    int num1 = 0;
    int zeroNo = 0;
    for (int i = numStr.length() - 1; i > 0; i--) {// 去除最后为零的数字
        if(numStr.charAt(i)=='.')break;
        num1 = Integer.parseInt(String.valueOf(numStr.charAt(i)));
        if (num1 == 0) {
        zeroNo++;
        } else {
        break;
        }
    }
    numStr = numStr.substring(0, numStr.length() - zeroNo);
    String toPoint = numStr;
    String toPointEnd = "";
    if (numStr.indexOf(".") != -1) {
        toPoint = numStr.substring(0, numStr.indexOf("."));
        toPointEnd = numStr.substring(numStr.indexOf(".") + 1);
    }
    // numStr
    int len = toPoint.length();
    toPoint += toPointEnd;
    boolean oneZero = false;// 去除如“00234”前面的零,判断数字中的零是否从第一个连续开始都为零
    for (int i = 0; i < toPoint.length(); i++) {
        num1 = Integer.parseInt(String.valueOf(toPoint.charAt(i)));
        if (num1 == 0) {
        if (i == 0) {
            oneZero = true;// 第一个数字为零
            continue;
        }
        if (oneZero) {// 从第一个为连续零
            continue;
        }
        } else {
        oneZero = false;// 之后不为连续
        }
        retNum += getNumOnSite(num1, len - i);
    }
    return (retNum.equals("")) ? "零元" : retNum;
    }
    
    /**
     * 判断此字符能否转成数字double类型
     * 
     * @param num
     *            字符串数字
     * @return 是数字为True
     */
    public static boolean isNum(String num) {
    try {
        double num1 = Double.parseDouble(num);
        num1++;// 让其计算下,没错就一定数数字
    } catch (Exception e) {
        // 只做捕获,不让程序死掉就行了
        return false;
    }
    return true;
    }
 
    /**
     * 数num在位置site上怎么读
     * 
     * @param num
     *            数
     * @param site
     *            位数
     * @return String 读法
     */
    private static String getNumOnSite(int num, int site) {
    String str = "";
    if (num == 0) {
        isZeroNum = true;// 记录数字为零
        if (site % 4 == 0) {
        isZero = true;// 记录千为为零
        }
        if (site % 4 == 1) {
        // 保险起见
        if (!isZero) {
            str = getSite(site);// 给了单位,就就不需零什么了,如四十万,零即清掉
        }
        isZeroNum = false;// 等于把前面记录有零的情况清掉
        }
        if (site < -2) {
        str = getNum(num);
        }// 厘单位后的数字
        return str;
    }
    if (isZeroNum) {
        str += getNum(0);
        isZeroNum = false;
        isZero = false;// 还原为假,
    }
    str += getNum(num) + getSite(site);
    return str;
    }
    private static String getNumDigOnSite(int num, int site) {
    String str = "";
    if(site==0){
        str += "点";
    }
    if (num == 0) {
        isZeroNum = true;// 记录数字为零
        if (site % 4 == 0) {
        isZero = true;// 记录千为为零
        }
        if (site % 4 == 1) {
        // 保险起见
        if (!isZero) {
            str = getSiteDig(site);// 给了单位,就就不需零什么了,如四十万,零即清掉
        }
        isZeroNum = false;// 等于把前面记录有零的情况清掉
        }
        if (site < -2) {
        str += getNum(num);
        }// 厘单位后的数字
        return str;
    }
    if (isZeroNum) {
        str += getNum(0);
        isZeroNum = false;
        isZero = false;// 还原为假,
    }
    str += getNum(num) + getSiteDig(site);
    return str;
    }
    /**
     * 得到数字对应的大写字
     * 
     * @param num
     *            数字(0-9)
     * @return String
     */
    private static String getNum(int num) {
    if (num > 9) {
        return "";
    }
    String str = "零壹贰叁肆伍陆柒捌玖";
    return str.substring(num, num + 1);
    }
private static String getSiteDig(int site){
    String str = "";
    if (site < 2) {
        str = "";
//        switch (site) {
//        case -2:
//        //str = "";
//        break;
//        case -1:
//        //str = "";
//        break;// 这上面写出小数点后的单位情况
//        }
        return str;
    }
    String siteStr="仟佰拾兆仟佰拾亿仟佰拾万仟佰拾";
    if(site>siteStr.length()){
        return "单位不知";
    }
    return String.valueOf(siteStr.charAt(siteStr.length()-site+1));
}
    /**
     * 得到位置所对应的单位
     * 
     * @param site
     *            位置数小数点前的
     * @return String
     */
    private static String getSite(int site) {
    String str = "";
    if (site < 0) {
        str = "";
        switch (site) {
        case -2:
        str = "厘";
        break;
        case -1:
        str = "分";
        break;// 这上面写出小数点后的单位情况
        }
        return str;
    }
    int yu = site % 4;
    int chu = site / 4;
    switch (yu) {    
    case 1:
        switch (chu) {
        case 0:
        str = "元";
        break;
        case 1:
        str = "万";
        break;
        case 2:
        str = "亿";
        break;
        case 3:
        str = "兆";
        break; // 这里写上每递进的4位的单位
        }
        break;
    case 2:
        str = "拾";
        break;
    case 3:
        str = "佰";
        break;
    case 0:
        switch (chu) {
        case 0:
        str = "角";
        break;
        default:
        str = "仟";// 其他除4余零都是千了
        break;
        }
    }
    if (isZeroNum) {
        if (site % 4 != 1 || isZero) {// 如200000345的千字位千万为零,却读到万时不加万,即"";
        str = "";
        }
    } // 处理中间的零
    return str;
    }
 
    private void getLowerToUpper() throws JRScriptletException {
    String keyString = "";
    Double lowerMoney = 0.0;
    String upperMoney ="";
    String[] lowerStr = null;
    for (Object obj : variablesMap.keySet()) {
        keyString = String.valueOf(obj).trim();
        lowerStr = keyString.split("_");
        if (lowerStr[0].equals("lowerMoney")) {
        lowerMoney = getVariableValue("lowerMoney_" + lowerStr[1]) == null ? new Double(
            0.0)
            : java.lang.Double.parseDouble(String.valueOf(getVariableValue("lowerMoney_"
                + lowerStr[1])));
        upperMoney = getChineseMoney(lowerMoney + "");
        if (hasPara("upperMoney_" + lowerStr[1],variablesMap))
            this.setVariableValue("upperMoney_" + lowerStr[1],
                upperMoney);
        }else if(lowerStr[0].equals("lowerDigit")) {
        lowerMoney = getVariableValue("lowerDigit_" + lowerStr[1]) == null ? new Double(
            0.0)
            : (java.lang.Double) getVariableValue("lowerDigit_"
                + lowerStr[1]);
        upperMoney = getChineseDigit(lowerMoney + "");
        if (hasPara("upperDigit_" + lowerStr[1],variablesMap))
            this.setVariableValue("upperDigit_" + lowerStr[1],
                upperMoney);
        }
    }
    }
 
    public static boolean hasPara(String para,Map<String, JRFillVariable>  vMap){//Map<String, Object>
    for (Object obj:vMap.keySet()) {
        if(para.equals(obj.toString())) return true;
    }
    return false;
    }
    /**
     * Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void afterColumnInit() throws JRScriptletException {
    // getLowerToUpper();
    super.afterColumnInit();
    }
 
    /**
     * End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't
     * modify or move please!
     */
    public void afterDetailEval() throws JRScriptletException {
    getLowerToUpper();
    super.afterDetailEval();
    }
 
    /**
     * End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void afterGroupInit(String groupName) throws JRScriptletException {
//    System.out.println(groupName);
    super.afterGroupInit(groupName);
    }
 
    /**
     * End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void afterPageInit() throws JRScriptletException {
    // getLowerToUpper();
    super.afterPageInit();
    }
 
    /**
     * End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify
     * or move please!
     */
    /**
     * Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void afterReportInit() throws JRScriptletException {
    // getLowerToUpper();
    super.afterReportInit();
    }
 
    /**
     * End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void beforeColumnInit() throws JRScriptletException {
    // getLowerToUpper();
    }
 
    /**
     * End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't
     * modify or move please!
     */
    public void beforeDetailEval() throws JRScriptletException {
    // getLowerToUpper();
    }
 
    /** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment */
 
    /**
     * End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void beforeGroupInit(String groupName) throws JRScriptletException {
 
    }
 
    /**
     * End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void beforePageInit() throws JRScriptletException {
    // getLowerToUpper();
    }
 
    /**
     * End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    /**
     * Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't
     * modify or move please!
     */
    public void beforeReportInit() throws JRScriptletException {
    // getLowerToUpper();
    }
 
    public static void main(String[] args) {
    System.out.println(getSiteDig(17));
    System.out.println(toUpperNum("300304.11"));
    System.out.println(toUpperMoneyNum("300304.11"));
    }
}