Browse Source

[fix] 通用模块

重写金额大小写转换工具类:解决之前带角和分出现的多余值:零零角;解决例如.06和.60这种带小数的数值区分不了的问题
修改采购订单导出供应商合同后端接口,使用新的金额工具类
dev
liuxiaoxu 2 months ago
parent
commit
7fdbd8a414
  1. 2
      ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchaseOrderServiceImpl.java
  2. 114
      ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java

2
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()) .map(item -> item.getMaterialRealRmbSum())
.reduce(BigDecimal.ZERO, BigDecimal::add); .reduce(BigDecimal.ZERO, BigDecimal::add);
//转换成中文金额 //汉字大写合计金额 //转换成中文金额 //汉字大写合计金额
String ChineseCharacterAmount = MoneyUtils.toChineseCapitalized(totalAmount); String ChineseCharacterAmount = MoneyUtils.amountConversion(totalAmount.toString());
AtomicInteger index = new AtomicInteger(1); AtomicInteger index = new AtomicInteger(1);
for (PurchaseOrderChild item : purchaseOrderChildList) { for (PurchaseOrderChild item : purchaseOrderChildList) {
Map<String, Object> detailMap = new HashMap<>(); Map<String, Object> detailMap = new HashMap<>();

114
ruoyi-common/src/main/java/com/ruoyi/common/utils/MoneyUtils.java

@ -1,61 +1,97 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
* 金额大小写转换
* @author 刘晓旭
* */
public class MoneyUtils { public class MoneyUtils {
private static final String[] CHINESE_DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] UNITS = {"", "拾", "佰", "仟", "万", "亿"};
public static String toChineseCapitalized(BigDecimal amount) { private static final String[] cnNums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; // 汉字的数字
if (amount == null || amount.compareTo(BigDecimal.ZERO) == 0) { private static final String[] cnIntRadice = { "", "拾", "佰", "仟" }; // 基本单位
return "零元整"; private static final String[] cnIntUnits = { "", "万", "亿", "兆" }; // 对应整数部分扩展单位
private static final String[] cnDecUnits = { "角", "分"}; // 对应小数部分单位
private static final String cnInteger = "整"; // 整数金额时后面跟的字符
private static final String cnIntLast = "元"; // 整型完以后的单位
/**
* 金额大小写转换将数字转换为汉字
* @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];
StringBuilder result = new StringBuilder();
boolean isNegative = amount.signum() == -1;
amount = amount.abs();
// 将金额转换为整数部分和小数部分 if("0".equals(integer)){
long integerPart = amount.longValue(); result = "零"+cnIntLast;
BigDecimal decimalPart = amount.remainder(BigDecimal.ONE).setScale(2, BigDecimal.ROUND_HALF_UP); }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;
}
// 处理整数部分 //组装小数部分
int unitIndex = 0; if ("00".equals(point)) {
while (integerPart > 0) { result += cnInteger;
int digit = (int) (integerPart % 10); }else{
if (digit != 0 || result.length() > 0) { // 避免多个"零" String n="";
result.insert(0, UNITS[unitIndex]); 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];
} }
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("分");
}
}else{ }else{
result.append("整"); return "请检查金额(整数位不能超过16位,小数不能超过2位)";
} }
if (isNegative) { return result;
result.insert(0, "负");
} }
return result.toString();
} }
}

Loading…
Cancel
Save