来自豆包,靠谱
import java.math.BigDecimal; import java.math.RoundingMode; public class MoneyToChiness { private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UPPER_UNIT = {"", "拾", "佰", "仟"}; private static final String[] CN_GROUP = {"", "万", "亿"}; public static String moneyToChinese(BigDecimal i_money) { if (i_money.equals(BigDecimal.ZERO)) { return "零圆整"; } Double max = 1000000000000D; Double min = 0.01D; if (i_money.doubleValue() >= max || i_money.doubleValue() < min) { return "大于1万亿或小于1分了"; } i_money = i_money.setScale(2, RoundingMode.HALF_UP); String numStr = i_money.toString(); int pointPos = numStr.indexOf('.'); String s_int = null; String s_point = null; if (pointPos >= 0) { s_int = numStr.substring(0, pointPos); s_point = numStr.substring(pointPos + 1); } else { s_int = numStr; } StringBuilder sb = new StringBuilder(); if ("0".equals(s_int)) { sb.append("零元整"); } else { int groupCount = (int) Math.ceil(s_int.length() / 4.0); for (int group = 0; group < groupCount; group++) { boolean zeroFlag = true; boolean noZeroFlag = false; int start = (s_int.length() % 4 == 0? 0 : (s_int.length() % 4 - 4)) + 4 * group; for (int i = 0; i < 4; i++) { if (i + start >= 0) { int value = s_int.charAt(i + start) - '0'; if (value > 0) { sb.append(CN_UPPER_NUMBER[value]); if (i < 3) { sb.append(CN_UPPER_UNIT[i]); } zeroFlag = true; noZeroFlag = true; } else if (zeroFlag) { sb.append('零'); zeroFlag = false; } } } if (sb.charAt(sb.length() - 1) == '零') { sb.deleteCharAt(sb.length() - 1); } if (noZeroFlag || groupCount - group == 1) { sb.append(CN_GROUP[groupCount - group - 1]); } } sb.append('元'); if (s_point == null || "00".equals(s_point)) { sb.append('整'); } else { int j = s_point.charAt(0) - '0'; int f = s_point.charAt(1) - '0'; sb.append(CN_UPPER_NUMBER[j]).append('角'); sb.append(CN_UPPER_NUMBER[f]).append('分'); } } return sb.toString(); } public static void main(String[] args) { BigDecimal amount = new BigDecimal("1234.56"); System.out.println(moneyToChinese(amount)); } }
标签:中文,CN,int,阿拉伯数字,大写,money,sb,append,String From: https://www.cnblogs.com/luodengxiong/p/18631409