java 数字转汉字

    public static String int2chineseNum(int src) {
        final String num[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String unit[] = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        while(src > 0) {
            dst = (num[src % 10] + unit[count]) + dst;
            src = src / 10;
            count++;
        }
        return dst.replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                .replaceAll("零+", "零").replaceAll("零$", "");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现中文金额的示例代码: ``` public static String numberToChinese(double number) { String[] units = new String[]{"", "万", "亿", "万亿"}; String[] nums = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; String[] cents = new String[]{"角", "分"}; long temp = Math.round(number * 100); int numIndex = 0; int unitIndex = units.length - 1; boolean isZero = true; StringBuilder builder = new StringBuilder(); while (temp != 0) { int digit = (int) (temp % 10); if (digit != 0) { builder.insert(0, units[unitIndex]); builder.insert(0, nums[digit]); isZero = false; } else { if (!isZero) { builder.insert(0, nums[digit]); } isZero = true; } temp /= 10; numIndex++; if (numIndex % 4 == 0) { unitIndex--; } } if (builder.length() == 0) { builder.append(nums[0]); } builder.append("元"); temp = Math.round(number * 100); int centIndex = 0; while (temp != 0 && centIndex < cents.length) { int digit = (int) (temp % 10); if (digit != 0) { builder.append(nums[digit]).append(cents[centIndex]); } temp /= 10; centIndex++; } if (builder.length() == 1) { builder.append(nums[0]).append("角"); } return builder.toString(); } ``` 示例调用: ``` System.out.println(numberToChinese(123456789.12)); // 输出:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元壹角贰分 ``` 代码说明: 该实现将数字按照“亿、万、个”的单位进行分组,然后将每组的数字成中文表示。在换过程中,将0省略,同时在每个数字后面添加相应的单位。最后将整数部分和小数部分拼接起来,返回中文金额的字符串表示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值