大写金额赋值优化
This commit is contained in:
parent
485f13a907
commit
f32edfc7da
|
@ -128,9 +128,6 @@ public class N_F3_SAVE extends N_BASE_ACTION {
|
|||
private static final String ZERO_YUAN = "零元整";
|
||||
|
||||
public static String toChineseAmount(BigDecimal number) {
|
||||
// if (number == null || number.compareTo(BigDecimal.ZERO) < 0) {
|
||||
// throw new IllegalArgumentException("请输入非负数字");
|
||||
// }
|
||||
boolean isNegative = number.compareTo(BigDecimal.ZERO) < 0;
|
||||
if (isNegative) {
|
||||
number = number.abs(); // 转为正数
|
||||
|
@ -149,6 +146,8 @@ public class N_F3_SAVE extends N_BASE_ACTION {
|
|||
} else {
|
||||
char[] intChars = integerPart.toCharArray();
|
||||
int len = intChars.length;
|
||||
int zeroCount = 0; // 记录连续的零的个数
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
int digit = intChars[i] - '0';
|
||||
int digitPosition = len - i - 1;
|
||||
|
@ -156,18 +155,33 @@ public class N_F3_SAVE extends N_BASE_ACTION {
|
|||
int positionInSection = digitPosition % 4;
|
||||
|
||||
if (digit != 0) {
|
||||
// 如果前面有连续的零,添加一个零
|
||||
if (zeroCount > 0) {
|
||||
chineseInteger.append(CHINESE_CHARS[0]);
|
||||
zeroCount = 0;
|
||||
}
|
||||
|
||||
chineseInteger.append(CHINESE_CHARS[digit]);
|
||||
if (positionInSection > 0) {
|
||||
chineseInteger.append(DIGIT_UNITS[positionInSection]);
|
||||
}
|
||||
}
|
||||
if (positionInSection == 0) {
|
||||
if (digit != 0 || chineseInteger.length() == 0) {
|
||||
|
||||
// 每四位添加一个大数单位
|
||||
if (positionInSection == 0) {
|
||||
chineseInteger.append(LARGE_NUMBERS[section]);
|
||||
}
|
||||
} else {
|
||||
zeroCount++;
|
||||
|
||||
// 每四位的末尾,即使是零也要添加大数单位
|
||||
if (positionInSection == 0 && zeroCount < 4) {
|
||||
chineseInteger.append(LARGE_NUMBERS[section]);
|
||||
}
|
||||
}
|
||||
if (digit == 0) {
|
||||
chineseInteger.append(CHINESE_CHARS[digit]);
|
||||
|
||||
// 每四位的末尾重置零计数
|
||||
if (positionInSection == 0) {
|
||||
zeroCount = 0;
|
||||
}
|
||||
}
|
||||
// 移除多余的零和重复的单位
|
||||
|
@ -214,6 +228,11 @@ public class N_F3_SAVE extends N_BASE_ACTION {
|
|||
return ZERO_YUAN;
|
||||
}
|
||||
|
||||
// 添加负号(如果需要)
|
||||
if (isNegative) {
|
||||
return "负" + chineseInteger.toString();
|
||||
}
|
||||
|
||||
return chineseInteger.toString();
|
||||
}
|
||||
|
||||
|
@ -242,6 +261,14 @@ public class N_F3_SAVE extends N_BASE_ACTION {
|
|||
}
|
||||
}
|
||||
}
|
||||
// 移除连续的大数单位
|
||||
for (String largeNum : LARGE_NUMBERS) {
|
||||
if (!largeNum.isEmpty()) {
|
||||
while (sb.indexOf(largeNum + largeNum) != -1) {
|
||||
sb.replace(sb.indexOf(largeNum + largeNum), sb.indexOf(largeNum + largeNum) + largeNum.length() * 2, largeNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue