JAVA实现数字金额转化为中文大写金额

废话不多说,直接上代码

public class RmbChangeUtil {

    private static String[] nums = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

    private static String[] unit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};

    private static String[] countNum = {"角", "分"};


    public static String change(String money) {
        // 过滤空
        if (money == null || "".equals(money)) {
            return "";
        }
        StringBuilder res = new StringBuilder();
        String[] splitStr = money.split("\\.");
        if (splitStr.length > 2) {
            throw new RuntimeException("输入的参数不是数字!");
        }
        String front = splitStr[0];
        // 用于判定0的显示
        boolean isZero = true;
        if (front.length() > unit.length) {
            throw new RuntimeException("输入的参数大于万亿!");
        }
        for (int i = 0; i < front.length(); i++) {
            // 整数位处理
            int dw = front.length() - i - 1;
            // 用ASCII码获得数字
            int index = (front.charAt(i) - '0');
            if (index == 0) {
                isZero = true;
                if (dw == 0 || dw == 4  || dw == 8) {
                    // 元,万,亿需要拼接
                    res.append(unit[dw]);
                }
            } else {
                if (isZero && i != 0) {
                    // 多个0只显示一个
                    res.append(nums[0]);
                }
                isZero = false;
                res.append(nums[index]);
                res.append(unit[dw]);
            }
        }
        // 判断是否有小数位
        if (splitStr.length > 1) {
            // 小数位处理,如果不需要则直接删除这个if及内部所有内容
            isZero = true;
            String back = splitStr[1];
            if (back.length() > countNum.length) {
                throw new RuntimeException("小数位小于【" + countNum[countNum.length - 1] + "】!");
            }
            for (int i = 0; i < back.length(); i++) {
                int index = (back.charAt(i) - '0');
                if (index == 0) {
                    isZero = true;
                } else {
                    if (isZero && i != 0) {
                        res.append(nums[0]);
                    }
                    isZero = false;
                    res.append(nums[index]);
                    res.append(countNum[i]);
                }
            }
        }
        return res.toString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现人民币金额换成大写中文的完整源码: ```java import java.math.BigDecimal; public class RMBAmountToChinese { private static final String[] CN_NUM = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; // 中文数字 private static final String[] CN_UNIT = {"", "拾", "佰", "仟", "万", "亿"}; // 中文单位 private static final BigDecimal HUNDRED = new BigDecimal("100"); // 100 private static final BigDecimal TEN_THOUSAND = new BigDecimal("10000"); // 10000 private static final BigDecimal ONE_HUNDRED_MILLION = new BigDecimal("100000000"); // 1亿 /** * 将人民币金额换成大写中文 * @param amount 人民币金额,最大支持1亿 * @return 大写中文金额 */ public static String rmbAmountToChinese(BigDecimal amount) { StringBuilder sb = new StringBuilder(); amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP); // 保留2位小数并四舍五入 if (amount.compareTo(ONE_HUNDRED_MILLION) >= 0) { // 1亿及以上 BigDecimal quotient = amount.divide(ONE_HUNDRED_MILLION, BigDecimal.ROUND_DOWN); // 商 BigDecimal remainder = amount.remainder(ONE_HUNDRED_MILLION); // 余数 sb.append(convert(quotient)).append("亿"); if (remainder.compareTo(BigDecimal.ZERO) > 0) { sb.append(convert(remainder)); } } else { // 1亿以下 sb.append(convert(amount)); } return sb.toString(); } // 将数字换成大写中文 private static String convert(BigDecimal num) { StringBuilder sb = new StringBuilder(); String str = num.toString(); int index = str.indexOf("."); // 小数点位置 if (index == -1) { // 整数 index = str.length(); } String integerPart = str.substring(0, index); // 整数部分 String decimalPart = str.substring(index + 1); // 小数部分 int length = integerPart.length(); boolean zero = false; // 是否有过连续的零 for (int i = 0; i < length; i++) { int digit = integerPart.charAt(i) - '0'; // 当前数字 if (digit == 0) { // 当前数字是0 if (!zero && length - i < CN_UNIT.length) { // 如果前面没有连续的零,并且数字不是在万或亿的单位,则加上零 sb.append(CN_NUM[digit]); zero = true; } } else { // 当前数字不是0 if (zero) { // 如果前面有连续的零,则加上零 sb.append(CN_NUM[0]); zero = false; } sb.append(CN_NUM[digit]).append(CN_UNIT[length - i - 1]); } } if (decimalPart.equals("00")) { // 没有小数部分 sb.append("整"); } else { // 有小数部分 sb.append(CN_NUM[decimalPart.charAt(0) - '0']).append("角"); if (decimalPart.length() > 1 && decimalPart.charAt(1) != '0') { sb.append(CN_NUM[decimalPart.charAt(1) - '0']).append("分"); } } return sb.toString(); } public static void main(String[] args) { BigDecimal amount = new BigDecimal("1234567.89"); // 人民币金额 String chineseAmount = rmbAmountToChinese(amount); // 换成中文金额 System.out.println(chineseAmount); // 输出结果:壹佰贰十三万肆仟伍佰陆拾柒元捌角玖分 } } ``` 通过以上代码,我们可以将一个人民币金额换为大写中文金额。其中,我们使用了BigDecimal类进行精度计算,同时使用了StringBuilder类进行字符串拼接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值