|
@@ -1,130 +1,107 @@
|
|
|
package org.springblade.manager.utils;
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
+import java.text.NumberFormat;
|
|
|
|
|
|
public class AmountToChineseConverter {
|
|
|
|
|
|
- private static final String[] CHINESE_DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
|
|
- private static final String[] UNITS = {"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
|
|
|
- private static final String[] DECIMAL_UNITS = {"角", "分"};
|
|
|
-
|
|
|
- public static String convertToChinese(BigDecimal amount) {
|
|
|
- if (amount.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
- throw new IllegalArgumentException("Amount must be non-negative");
|
|
|
- }
|
|
|
-
|
|
|
- // 将BigDecimal转换为字符串并分割整数部分和小数部分
|
|
|
- String amountStr = amount.toPlainString();
|
|
|
- int dotIndex = amountStr.indexOf('.');
|
|
|
- String integerPartStr = dotIndex > 0 ? amountStr.substring(0, dotIndex) : amountStr;
|
|
|
- String decimalPartStr = dotIndex > 0 ? amountStr.substring(dotIndex + 1) : "";
|
|
|
-
|
|
|
- // 处理整数部分
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- if (integerPartStr.equals("0")) {
|
|
|
- result.append("零元整");
|
|
|
- } else {
|
|
|
- result.append(convertIntegerPart(new BigDecimal(integerPartStr))).append("元");
|
|
|
- }
|
|
|
-
|
|
|
- // 处理小数部分
|
|
|
- if (!decimalPartStr.isEmpty()) {
|
|
|
- String decimalResult = convertDecimalPart(decimalPartStr);
|
|
|
- if (!decimalResult.isEmpty()) {
|
|
|
- result.append(decimalResult);
|
|
|
- } else {
|
|
|
- result.append("整");
|
|
|
+ public static String upperCase2(double money){
|
|
|
+ String[] upNum = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
|
|
|
+ String[] danwei = {"圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};
|
|
|
+ //取消科学记数法
|
|
|
+ NumberFormat numFormat = NumberFormat.getInstance();
|
|
|
+ numFormat.setMaximumFractionDigits(2);//设置小数位个数
|
|
|
+ numFormat.setGroupingUsed(false);//取消科学技术发
|
|
|
+ String formatNum = numFormat.format(money);
|
|
|
+ String strmoney = formatNum + "";//浮点型转为字符型
|
|
|
+ String lastUpNum = "null"; //用于存放上个参数的值
|
|
|
+ String result = "";//返回的结果
|
|
|
+ String[] split = strmoney.split("\\.");
|
|
|
+ String strMoney = split[0];
|
|
|
+ String point = "";
|
|
|
+ //小数部分取值处理。
|
|
|
+ if(split.length>1){
|
|
|
+ point = split[1];
|
|
|
+ if(point.length()==1){
|
|
|
+ point = point.concat("0");
|
|
|
}
|
|
|
- } else {
|
|
|
- result.append("整");
|
|
|
+ }else {
|
|
|
+ point = "0";
|
|
|
}
|
|
|
-
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- private static String convertIntegerPart(BigDecimal number) {
|
|
|
- if (number.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
- return "";
|
|
|
+ //大于12位就直接返回。
|
|
|
+ int moneyLen = strMoney.length();
|
|
|
+ if(money==0){
|
|
|
+ return "零圆整";
|
|
|
}
|
|
|
-
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- String numberStr = number.toPlainString();
|
|
|
-
|
|
|
- // 分割成亿、万和个位部分
|
|
|
- String yiPart = numberStr.length() > 8 ? numberStr.substring(0, numberStr.length() - 8) : "";
|
|
|
- String wanPart = numberStr.length() > 4 ? numberStr.substring(numberStr.length() - 8, numberStr.length() - 4) : "";
|
|
|
- String gePart = numberStr.length() > 4 ? numberStr.substring(numberStr.length() - 4) : numberStr;
|
|
|
-
|
|
|
- if (!yiPart.isEmpty()) {
|
|
|
- result.append(convertPart(yiPart)).append("亿");
|
|
|
- }
|
|
|
- if (!wanPart.isEmpty()) {
|
|
|
- result.append(convertPart(wanPart)).append("万");
|
|
|
+ if(moneyLen>12){
|
|
|
+ return "金额:"+money+"元,超出大写转换范围。最大金额:999999999999.99元";
|
|
|
}
|
|
|
- if (!gePart.isEmpty()) {
|
|
|
- result.append(convertPart(gePart));
|
|
|
- }
|
|
|
-
|
|
|
- // 去掉多余的“零”
|
|
|
- while (result.indexOf("零") == result.lastIndexOf("零") && result.indexOf("零") != -1) {
|
|
|
- result.deleteCharAt(result.indexOf("零"));
|
|
|
- }
|
|
|
-
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- private static String convertPart(String part) {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- boolean hasNonZero = false;
|
|
|
-
|
|
|
- for (int i = 0; i < part.length(); i++) {
|
|
|
- char digitChar = part.charAt(part.length() - 1 - i);
|
|
|
- int digit = Character.digit(digitChar, 10);
|
|
|
-
|
|
|
- if (digit == 0) {
|
|
|
- if (hasNonZero) {
|
|
|
- result.insert(0, CHINESE_DIGITS[digit]);
|
|
|
- hasNonZero = false;
|
|
|
+ //整数(integer)部分处理。
|
|
|
+ if(!"0".equals(strMoney)){
|
|
|
+ for (int i = 0; i < moneyLen; i++) {
|
|
|
+ String strNum = strMoney.charAt(i)+"";
|
|
|
+ int singleNum = Integer.parseInt(strNum);
|
|
|
+ String upSingleNum = upNum[singleNum];
|
|
|
+ //上一为不等于0的情况
|
|
|
+ if(!"零".equals(lastUpNum)){
|
|
|
+ if(!"零".equals(upSingleNum)){
|
|
|
+ result = result.concat(upSingleNum).concat(danwei[moneyLen-i-1]);
|
|
|
+ }else
|
|
|
+ //为零但是在万、亿位上要加单位 (moneyLen-i)==9 指的是单位:亿。 (moneyLen-i)==5指的是单位:万
|
|
|
+ if( (moneyLen-i)==5 || (moneyLen-i)==9 ){
|
|
|
+ lastUpNum="";
|
|
|
+ }else {
|
|
|
+ result=result.concat(upSingleNum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //上一位为0的情况
|
|
|
+ if("零".equals(lastUpNum) && !"零".equals(upSingleNum)){
|
|
|
+ result = result.concat(upSingleNum).concat(danwei[moneyLen-i-1]);
|
|
|
+ }
|
|
|
+ //捕捉上一位数(lastUpNum)为零的情况做优化。
|
|
|
+ if((moneyLen-i)==5 || (moneyLen-i)==9 ){
|
|
|
+ //排除加单位时前面为"零"的情况。如:两百零万
|
|
|
+ if("零".equals(lastUpNum)||"null".equals(lastUpNum)){
|
|
|
+ result = result.substring(0,result.length()-1);
|
|
|
+ }
|
|
|
+ if(!result.endsWith("亿")){
|
|
|
+ result = result.concat(danwei[moneyLen-i-1]);
|
|
|
+ }
|
|
|
+ lastUpNum="";
|
|
|
+ }else {
|
|
|
+ //把当前大写数字复制给 lastUpNum 用于下次判断
|
|
|
+ lastUpNum = upSingleNum;
|
|
|
}
|
|
|
- } else {
|
|
|
- result.insert(0, CHINESE_DIGITS[digit] + UNITS[i]);
|
|
|
- hasNonZero = true;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- private static String convertDecimalPart(String decimalPartStr) {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
+ //对几万元整和几亿元整(result:五万零或者五亿零零)做优化。
|
|
|
+ result=result.replaceAll("零零","零");
|
|
|
+ if(result.endsWith("零")){
|
|
|
+ String substring = result.substring(0,result.length() - 1);
|
|
|
+ result = substring;
|
|
|
+ }
|
|
|
+ result = result.concat("圆");
|
|
|
+ result = result.replaceAll("圆圆","圆");
|
|
|
+ result = result.replaceAll("万万","万");
|
|
|
|
|
|
- // 确保小数部分最多两位
|
|
|
- if (decimalPartStr.length() > 2) {
|
|
|
- decimalPartStr = decimalPartStr.substring(0, 2);
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < decimalPartStr.length(); i++) {
|
|
|
- char digitChar = decimalPartStr.charAt(i);
|
|
|
- int digit = Character.digit(digitChar, 10);
|
|
|
-
|
|
|
- if (digit > 0) {
|
|
|
- result.append(CHINESE_DIGITS[digit]).append(DECIMAL_UNITS[i]);
|
|
|
+ //小数(point)部分处理
|
|
|
+ if("0".equals(point)){
|
|
|
+ result = result+"整";
|
|
|
+ }else {
|
|
|
+ //去 整
|
|
|
+// if(result.endsWith("整")){
|
|
|
+// result = result.substring(0,result.length()-1);
|
|
|
+// }
|
|
|
+ if((point.charAt(0)+"").equals("0")){
|
|
|
+ result = result.concat(upNum[Integer.parseInt(point.charAt(1)+"")]+"分");
|
|
|
+ }else if((point.charAt(1)+"").equals("0")){
|
|
|
+ result = result.concat(upNum[Integer.parseInt(point.charAt(0)+"")]+"角");
|
|
|
+ }else {
|
|
|
+ result = result.concat(upNum[Integer.parseInt(point.charAt(0)+"")]+"角").concat(upNum[Integer.parseInt(point.charAt(1)+"")]+"分");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 如果小数部分为空,添加“整”
|
|
|
- if (result.length() == 0) {
|
|
|
- result.append("整");
|
|
|
- }
|
|
|
-
|
|
|
- return result.toString();
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- BigDecimal amount = new BigDecimal("713.12");
|
|
|
- System.out.println(amount + " 转换为中文大写: " + convertToChinese(amount));
|
|
|
|
|
|
- BigDecimal largeAmount = new BigDecimal("121234567890123.45");
|
|
|
- System.out.println(largeAmount + " 转换为中文大写: " + convertToChinese(largeAmount));
|
|
|
- }
|
|
|
}
|