From 520f76ec0569ac605156e4ac1527a32e4d294d8a Mon Sep 17 00:00:00 2001 From: dimengzhe <251008545@qq.com> Date: Thu, 18 Aug 2022 10:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=80=E5=94=AE=E8=AE=A2=E5=8D=95=E5=90=88?= =?UTF-8?q?=E5=90=8C=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/component/DocPdfComponent.java | 4 +- .../base/utils/MoneyToChineseUtils.java | 220 ++++++++++++++++++ 2 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/utils/MoneyToChineseUtils.java diff --git a/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/DocPdfComponent.java b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/DocPdfComponent.java index 0b9e8e6..3d04d0e 100644 --- a/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/DocPdfComponent.java +++ b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/DocPdfComponent.java @@ -1,5 +1,6 @@ package com.yxt.common.base.config.component; +import cn.hutool.core.date.DateUtil; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.yxt.common.base.utils.DateUtils; @@ -43,7 +44,8 @@ public class DocPdfComponent { * @return 返回word文件路径和链接路径 */ public Map creatWord(Map map, String typeName, String fileName) { - String dateStr = DateUtils.dateConvertStr(new Date(), "yyyyMMdd"); +// String dateStr = DateUtils.dateConvertStr(new Date(), "yyyyMMdd"); + String dateStr = DateUtil.format(new Date(), "yyyyMMdd"); String targetPath = uploadTemplateUrl + dateStr; try { //Configuration 用于读取ftl文件 diff --git a/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/utils/MoneyToChineseUtils.java b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/utils/MoneyToChineseUtils.java new file mode 100644 index 0000000..75cdfed --- /dev/null +++ b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/utils/MoneyToChineseUtils.java @@ -0,0 +1,220 @@ +package com.yxt.common.base.utils; + +import java.math.BigDecimal; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @Author dimengzhe + * @Date 2022/8/17 11:26 + * @Description + */ +public class MoneyToChineseUtils { + + /** + * 不考虑分隔符的正确性 + */ + private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$"); + private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray(); + private static final String[] UNITS = {"元", "角", "分", "整"}; + private static final String[] U1 = {"", "拾", "佰", "仟"}; + private static final String[] U2 = {"", "万", "亿"}; + + /** + * 将金额(整数部分等于或少于12位,小数部分2位)转换为中文大写形式. + * + * @param money 金额数字 + * @return 中文大写 + */ + public static String convert(Double money) { + money = Math.abs(money); + if (Double.valueOf(0).equals(money)) { + return "零元整"; + } + String amount = moneyFormat(money); + // 去掉分隔符 + amount = amount.replace(",", ""); + // 验证金额正确性 + + Matcher matcher = AMOUNT_PATTERN.matcher(amount); + if (!matcher.find()) { + + } + // 整数部分 + String integer = matcher.group(1); + // 小数部分 + String fraction = matcher.group(2); + + String result = ""; + if (!integer.equals("0")) { + // 整数部分 + result += integer2rmb(integer) + UNITS[0]; + } + if (fraction.equals("00")) { + // 添加[整] + result += UNITS[3]; + } else if (fraction.startsWith("0") && integer.equals("0")) { + // 去掉分前面的[零] + result += fraction2rmb(fraction).substring(1); + } else { + // 小数部分 + result += fraction2rmb(fraction); + } + return result; + } + + /** + * 将金额小数部分转换为中文大写 + */ + private static String fraction2rmb(String fraction) { + // 角 + char jiao = fraction.charAt(0); + // 分 + char fen = fraction.charAt(1); + return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : "")) + (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : ""); + } + + /** + * 将金额整数部分转换为中文大写 + * + * @param integer + * @return + */ + private static String integer2rmb(String integer) { + StringBuilder buffer = new StringBuilder(); + // 从个位数开始转换 + int i, j; + for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) { + char n = integer.charAt(i); + if (n == '0') { + // 当n是0且n的右边一位不是0时,插入[零] + if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') { + buffer.append(RMB_NUMS[0]); + } + // 插入[万]或者[亿] + if (j % 4 == 0) { + if (i > 0 && integer.charAt(i - 1) != '0' + || i > 1 && integer.charAt(i - 2) != '0' + || i > 2 && integer.charAt(i - 3) != '0') { + buffer.append(U2[j / 4]); + } + } + } else { + if (j % 4 == 0) { + // 插入[万]或者[亿] + buffer.append(U2[j / 4]); + } + // 插入[拾]、[佰]或[仟] + buffer.append(U1[j % 4]); + // 插入数字 + buffer.append(RMB_NUMS[n - '0']); + } + } + return buffer.reverse().toString(); + } + + /** + * 对金额的格式调整到分 + * + * @param preMoney + * @return + */ + public static String moneyFormat(Double preMoney) { + //23->23.00 + String money = preMoney.toString(); + StringBuffer sb = new StringBuffer(); + if (money == null) { + return "0.00"; + } + int index = money.indexOf("."); + if (index == -1) { + return money + ".00"; + } else { + //整数部分 + String s0 = money.substring(0, index); + //小数部分 + String s1 = money.substring(index + 1); + if (s1.length() == 1) { + //小数点后一位 + s1 = s1 + "0"; + } else if (s1.length() > 2) { + //如果超过3位小数,截取2位就可以了 + s1 = s1.substring(0, 2); + } + sb.append(s0); + sb.append("."); + sb.append(s1); + } + return sb.toString(); + } + + /** + * 对金额的格式调整到分 + * + * @param preMoney + * @return + */ + public static String moneyFormat(BigDecimal preMoney) { + //23->23.00 + String money = preMoney.toString(); + StringBuffer sb = new StringBuffer(); + if (money == null) { + return "0.00"; + } + int index = money.indexOf("."); + if (index == -1) { + return money + ".00"; + } else { + //整数部分 + String s0 = money.substring(0, index); + //小数部分 + String s1 = money.substring(index + 1); + if (s1.length() == 1) { + //小数点后一位 + s1 = s1 + "0"; + } else if (s1.length() > 2) { + //如果超过3位小数,截取2位就可以了 + s1 = s1.substring(0, 2); + } + sb.append(s0); + sb.append("."); + sb.append(s1); + } + return sb.toString(); + } + + /** + * 对金额的格式调整到分 + * + * @param preMoney + * @return + */ + public static String moneyFormat(String preMoney) { + //23->23.00 + String money = preMoney; + StringBuffer sb = new StringBuffer(); + if (money == null) { + return "0.00"; + } + int index = money.indexOf("."); + if (index == -1) { + return money + ".00"; + } else { + //整数部分 + String s0 = money.substring(0, index); + //小数部分 + String s1 = money.substring(index + 1); + if (s1.length() == 1) { + //小数点后一位 + s1 = s1 + "0"; + } else if (s1.length() > 2) { + //如果超过3位小数,截取2位就可以了 + s1 = s1.substring(0, 2); + } + sb.append(s0); + sb.append("."); + sb.append(s1); + } + return sb.toString(); + } +}