From 7fdbd8a414d934e870ac80a008805dd6f1ac778c Mon Sep 17 00:00:00 2001 From: liuxiaoxu <1793812695@qq.com> Date: Thu, 26 Sep 2024 14:40:31 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E9=80=9A=E7=94=A8=E6=A8=A1=E5=9D=97=20?= =?UTF-8?q?=E9=87=8D=E5=86=99=E9=87=91=E9=A2=9D=E5=A4=A7=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=9A=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E4=B9=8B=E5=89=8D=E5=B8=A6=E8=A7=92=E5=92=8C=E5=88=86?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=9A=84=E5=A4=9A=E4=BD=99=E5=80=BC=EF=BC=9A?= =?UTF-8?q?=E9=9B=B6=E9=9B=B6=E8=A7=92=EF=BC=9B=E8=A7=A3=E5=86=B3=E4=BE=8B?= =?UTF-8?q?=E5=A6=82.06=E5=92=8C.60=E8=BF=99=E7=A7=8D=E5=B8=A6=E5=B0=8F?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E6=95=B0=E5=80=BC=E5=8C=BA=E5=88=86=E4=B8=8D?= =?UTF-8?q?=E4=BA=86=E7=9A=84=E9=97=AE=E9=A2=98=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=87=87=E8=B4=AD=E8=AE=A2=E5=8D=95=E5=AF=BC=E5=87=BA=E4=BE=9B?= =?UTF-8?q?=E5=BA=94=E5=95=86=E5=90=88=E5=90=8C=E5=90=8E=E7=AB=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84=E9=87=91?= =?UTF-8?q?=E9=A2=9D=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/PurchaseOrderServiceImpl.java | 2 +- .../com/ruoyi/common/utils/MoneyUtils.java | 122 ++++++++++++------ 2 files changed, 80 insertions(+), 44 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchaseOrderServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchaseOrderServiceImpl.java index 314ffce3..0e7e3dd3 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchaseOrderServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchaseOrderServiceImpl.java @@ -1116,7 +1116,7 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService .map(item -> item.getMaterialRealRmbSum()) .reduce(BigDecimal.ZERO, BigDecimal::add); //转换成中文金额 //汉字大写合计金额 - String ChineseCharacterAmount = MoneyUtils.toChineseCapitalized(totalAmount); + String ChineseCharacterAmount = MoneyUtils.amountConversion(totalAmount.toString()); AtomicInteger index = new AtomicInteger(1); for (PurchaseOrderChild item : purchaseOrderChildList) { Map detailMap = new HashMap<>(); diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java index 24e5fd64..f68decca 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java @@ -1,61 +1,97 @@ package com.ruoyi.common.utils; import java.math.BigDecimal; +import java.text.DecimalFormat; + +/** +* 金额大小写转换 +* @author 刘晓旭 +* */ public class MoneyUtils { - private static final String[] CHINESE_DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; - private static final String[] UNITS = {"", "拾", "佰", "仟", "万", "亿"}; - public static String toChineseCapitalized(BigDecimal amount) { - if (amount == null || amount.compareTo(BigDecimal.ZERO) == 0) { - return "零元整"; - } + private static final String[] cnNums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; // 汉字的数字 + private static final String[] cnIntRadice = { "", "拾", "佰", "仟" }; // 基本单位 + private static final String[] cnIntUnits = { "", "万", "亿", "兆" }; // 对应整数部分扩展单位 + private static final String[] cnDecUnits = { "角", "分"}; // 对应小数部分单位 + private static final String cnInteger = "整"; // 整数金额时后面跟的字符 + private static final String cnIntLast = "元"; // 整型完以后的单位 - StringBuilder result = new StringBuilder(); - boolean isNegative = amount.signum() == -1; - amount = amount.abs(); + /** + * 金额大小写转换,将数字转换为汉字 + * @param money 支持最大16位整数2位小数 + * @return + */ + public static String amountConversion(String money){ + //如果传入为空,则返回null + if(StringUtils.isBlank(money)){ + return "null"; + } + String result = ""; + //整数部分 + String integer=""; + //小数部分 + String point=""; + if(money.matches("^[0-9]{0,16}|[0-9]{0,16}+([.]{0,1}[0-9]{0,2})$")){ + BigDecimal bigDecimal = new BigDecimal(money); + //对金额进行格式化,统一小数点后面保留两位。 此举能去除整数部分多余的0,以及小数部分统一保留两位 如:00456.1-->456.10 + money = new DecimalFormat("##0.00").format(bigDecimal); + //整数部分 + integer = money.split("\\.").length==1?money:money.split("\\.")[0]; + //小数部分 + point = money.split("\\.").length==1?"00":money.split("\\.")[1]; - // 将金额转换为整数部分和小数部分 - long integerPart = amount.longValue(); - BigDecimal decimalPart = amount.remainder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP); - // 处理整数部分 - int unitIndex = 0; - while (integerPart > 0) { - int digit = (int) (integerPart % 10); - if (digit != 0 || result.length() > 0) { // 避免多个"零" - result.insert(0, UNITS[unitIndex]); + if("0".equals(integer)){ + result = "零"+cnIntLast; + }else{ + int zeroCount = 0; + char[] array = integer.toCharArray(); + String n=""; + int p=0,q=0,m=0; + for (int i = 0; i < integer.length(); i++) { + n = Character.toString(array[i]); + p = integer.length() - i - 1; + q = p / 4; + m = p % 4; + if (n.equals("0")) { + zeroCount++; + } else { + if (zeroCount > 0) { + result += cnNums[0]; + } + zeroCount = 0; // 归零 + result += cnNums[Integer.parseInt(n)] + cnIntRadice[m]; + } + if (m == 0 && zeroCount < 4) { + result += cnIntUnits[q]; + } + } + result += cnIntLast; } - result.insert(0, CHINESE_DIGITS[digit]); - integerPart /= 10; - unitIndex++; - if (unitIndex == 4) { - unitIndex = 0; - result.insert(0, "万"); - } - } - // 添加单位“元” - result.append("元"); - - // 处理小数部分 - if (decimalPart.compareTo(BigDecimal.ZERO) > 0) { - result.append("零").append(CHINESE_DIGITS[decimalPart.intValue()]); - result.append("角"); - BigDecimal cents = decimalPart.remainder(BigDecimal.ONE).multiply(BigDecimal.TEN); - if (cents.compareTo(BigDecimal.ZERO) > 0) { - result.append("零").append(CHINESE_DIGITS[cents.intValue()]); - result.append("分"); + //组装小数部分 + if ("00".equals(point)) { + result += cnInteger; + }else{ + String n=""; + char[] array = point.toCharArray(); + for (int i = 0; i < point.length(); i++) { + n = Character.toString(array[i]); + if (!n.equals("0")) { + result += cnNums[Integer.parseInt(n)] + cnDecUnits[i]; + } + } } - } else { - result.append("整"); - } - if (isNegative) { - result.insert(0, "负"); + + }else{ + return "请检查金额(整数位不能超过16位,小数不能超过2位)"; } - return result.toString(); + return result; } } + +