论文降重实战:从查重原理到AI工具应用
2026/8/8 10:30:37
文件名是AmountToChinese.java
import java.math.BigDecimal; import java.math.RoundingMode; public class AmountToChinese { private static final String[] DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] INTEGER_UNITS = {"", "拾", "佰", "仟"}; private static final String[] SECTION_UNITS = {"", "万", "亿", "兆"}; /** * 将数字金额转换为大写中文金额 * * @param amount 金额,支持两位小数 * @return 大写金额字符串,例如:1234.56 -> 壹仟贰佰叁拾肆元伍角陆分 */ public static String amountToChinese(BigDecimal amount) { if (amount == null) { throw new IllegalArgumentException("金额不能为空"); } if (amount.compareTo(BigDecimal.ZERO) < 0) { return "负" + amountToChinese(amount.abs()); } if (amount.compareTo(new BigDecimal("999999999999999.99")) > 0) { throw new IllegalArgumentException("金额超出支持范围"); } amount = amount.setScale(2, RoundingMode.HALF_UP); long integerPart = amount.longValue(); int decimalPart = amount.subtract(BigDecimal.valueOf(integerPart)) .movePointRight(2) .intValue(); int jiao = decimalPart / 10; int fen = decimalPart % 10; StringBuilder result = new StringBuilder(); if (integerPart == 0) { if (jiao == 0 && fen == 0) { return "零元整"; } result.append("零元"); } else { result.append(convertInteger(integerPart)).append("元"); } if (jiao == 0 && fen == 0) { result.append("整"); } else if (jiao == 0) { if (integerPart != 0) { result.append("零"); } result.append(DIGITS[fen]).append("分"); } else { result.append(DIGITS[jiao]).append("角"); if (fen != 0) { result.append(DIGITS[fen]).append("分"); } } return result.toString(); } /** * 将 double 类型金额转换为大写中文金额 */ public static String amountToChinese(double amount) { return amountToChinese(BigDecimal.valueOf(amount)); } private static String convertInteger(long num) { if (num == 0) { return "零"; } StringBuilder result = new StringBuilder(); boolean needZeroBetweenSections = false; int sectionIndex = 0; while (num > 0) { int section = (int) (num % 10000); if (section != 0) { if (needZeroBetweenSections && result.length() > 0) { result.insert(0, "零"); } result.insert(0, convertSection(section) + SECTION_UNITS[sectionIndex]); needZeroBetweenSections = section < 1000; } else { needZeroBetweenSections = true; } num /= 10000; sectionIndex++; } return result.toString(); } private static String convertSection(int section) { StringBuilder result = new StringBuilder(); boolean lastWasZero = true; int unitIndex = 0; while (section > 0) { int digit = section % 10; if (digit == 0) { if (!lastWasZero) { result.insert(0, "零"); lastWasZero = true; } } else { result.insert(0, DIGITS[digit] + INTEGER_UNITS[unitIndex]); lastWasZero = false; } unitIndex++; section /= 10; } return result.toString(); } public static void main(String[] args) { double[] testAmounts = { 0, 0.01, 0.1, 0.05, 0.56, 1, 10, 10.01, 100.5, 1001, 1234.56, 10000, 100000001, 101000000, 1234567890.12 }; System.out.println("数字金额 -> 大写金额"); System.out.println("----------------------------------------"); for (double amount : testAmounts) { System.out.printf("%-18s -> %s%n", formatAmount(amount), amountToChinese(amount)); } } private static String formatAmount(double amount) { return BigDecimal.valueOf(amount).setScale(2, RoundingMode.HALF_UP).toPlainString(); } }