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"));
|
}
|
}
|