Java数字转换为人民币的大写

5 篇文章 0 订阅

工具类:

import java.math.BigDecimal;

/**
 * 数字转换为人民币的大写
 */
public class NumberToCN {
    /**
     * 汉语中数字大写
     */
    private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
            "伍", "陆", "柒", "捌", "玖" };
    /**
     * 汉语中货币单位大写,这样的设计类似于占位符
     */
    private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
            "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
            "佰", "仟" };
    /**
     * 特殊字符:整
     */
    private static final String CN_FULL = "整";
    /**
     * 特殊字符:负
     */
    private static final String CN_NEGATIVE = "负";
    /**
     * 金额的精度,默认值为2
     */
    private static final int MONEY_PRECISION = 2;
    /**
     * 特殊字符:零元整
     */
    private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

    /**
     * 把输入的金额转换为汉语中人民币的大写
     *
     * @param numberOfMoney
     *            输入的金额
     * @return 对应的汉语大写
     */
    public static String numberToCn(BigDecimal numberOfMoney) {
        StringBuffer sb = new StringBuffer();
        // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情况
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        //这里会进行金额的四舍五入
        long number = numberOfMoney.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
        // 得到小数点后两位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;
        // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;
        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次获取到最后一个数
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            // 让number每次都去掉最后一个数
            number = number / 10;
            ++numIndex;
        }
        // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }
        // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
        if (!(scale > 0)) {
            sb.append(CN_FULL);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println("数字转换为人民币的大写测试");
        System.out.println("193847032471.23 ==》 " + NumberToCN.numberToCn(new BigDecimal(193847032471.23)));
    }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java代码将数字转换人民币大写的方法: ```java import java.text.DecimalFormat; public class RMBConverter { private static final String[] CN_NUM = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UNIT = {"", "拾", "佰", "仟"}; private static final String[] CN_SECTION = {"", "万", "亿", "万亿"}; public static String convert(double num) { if (num == 0) { return "零元整"; } long integer = (long) num; // 整数部分 StringBuilder sb = new StringBuilder(); int sectionIndex = 0; // 记录当前处理的是第几个节权位 while (integer > 0) { int section = (int) (integer % 10000); // 取最后四位 sb.insert(0, CN_SECTION[sectionIndex]); // 插入节权位 sb.insert(0, sectionToChinese(section)); // 插入节内数字 integer /= 10000; // 去掉已经处理的最后四位 sectionIndex++; // 移动到下一个节权位 } if (num < 0) { sb.insert(0, "负"); } DecimalFormat decimalFormat = new DecimalFormat("#.00"); String decimal = decimalFormat.format(num - (long) num); // 小数部分 if (decimal.equals("0.00")) { sb.append("整"); } else { sb.append(decimal.substring(2)).append("分"); sb.insert(0, decimalToChinese(decimal.substring(0, 2)) + "角"); } return sb.toString(); } private static String sectionToChinese(int section) { StringBuilder sb = new StringBuilder(); if (section == 0) { return CN_NUM[0]; } int unitIndex = 0; // 记录当前处理的是第几位 boolean zero = true; // 记录当前位是否为零 while (section > 0) { int digit = section % 10; // 取最后一位 if (digit == 0) { if (!zero) { sb.insert(0, CN_NUM[0]); // 插入零 zero = true; } } else { sb.insert(0, CN_UNIT[unitIndex]); // 插入单位 sb.insert(0, CN_NUM[digit]); // 插入数字 zero = false; } section /= 10; // 去掉已经处理的最后一位 unitIndex++; // 移动到下一位 } return sb.toString(); } private static String decimalToChinese(String decimal) { StringBuilder sb = new StringBuilder(); if (decimal.equals("00")) { return ""; } int digit1 = decimal.charAt(0) - '0'; int digit2 = decimal.charAt(1) - '0'; if (digit1 > 0) { sb.append(CN_NUM[digit1]).append("拾"); } else if (digit2 > 0) { sb.append("零"); } if (digit2 > 0) { sb.append(CN_NUM[digit2]); } return sb.toString(); } } ``` 使用方法如下: ```java double num = 123456789.12; String rmb = RMBConverter.convert(num); System.out.println(rmb); // 壹亿贰仟叁佰肆拾伍万陆仟柒百捌十玖元壹角贰分 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值