工具类篇-03-BigDecimal金额转中文大写

package com.greatmicro.ates.settle.core.util;
import java.math.BigDecimal;

/**
 *  金额工具类
 * @author Wang
 */
public class BigDecimalUtils {

    /**
     *  将数字BigDecimal转换成中文大写
     * 
     * @param bigDecimal  原始的金额 eg:11111.11
     * @return 中文金额 eg: 壹万壹仟壹佰壹拾壹元壹角壹分
     */
    public static String bigDecimalToString(BigDecimal bigDecimal) {
        String[] chinese = new String[]{"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
        String[] numChinese = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] afterChinese = new String[]{"角", "分"};
        String str = String.valueOf(bigDecimal);
        String[] arr = str.split("\\.");
        char[] chars = arr[0].toCharArray();
        StringBuilder builder = new StringBuilder();
 
        for(int i = 0; i < chars.length; ++i) {
            builder.append(numChinese[Integer.valueOf(String.valueOf(chars[i]))]).append(chinese[chars.length - i - 1]);
        }
 
        if (arr.length == 1) {
            return builder.toString() + "元整";
        } else if (arr[1].length() > 2) {
            throw new IllegalArgumentException("人民币大写转换BigDecimal只能保留2位小数");
        } else {
            builder.append("元");
            char[] chars1 = arr[1].toCharArray();
 
            for(int i = 0; i < chars1.length; ++i) {
                builder.append(numChinese[Integer.valueOf(String.valueOf(chars1[i]))]).append(afterChinese[i]);
            }
 
            return builder.toString();
        }
    }
}

2. 下面这个也不错

package com.greatmicro.ates.settle.core.util;

import lombok.extern.log4j.Log4j2;
import java.math.BigDecimal;

/**
 * 金额工具类
 *
 * @author Wang
 */
@Log4j2
public class BigDecimalUtils {


    private static final String UNIT = "万仟佰拾亿仟佰拾万仟佰拾元角分";
    private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
    private static final double MAX_VALUE = 9999999999999.99D;

    /**
     * 将数字BigDecimal转换成中文大写
     *
     * @param bigDecimal 原始的金额 eg:11111.11
     * @return 中文金额 eg: 壹万壹仟壹佰壹拾壹元壹角壹分
     */
    public static String bigDecimalToString(BigDecimal bigDecimal) {

        double v = bigDecimal.doubleValue();

        if (v < 0 || v > MAX_VALUE) {
            return "参数非法!";
        }
        long l = Math.round(v * 100);
        if (l == 0) {
            return "零元整";
        }
        String strValue = l + "";
        // i用来控制数
        int i = 0;
        // j用来控制单位
        int j = UNIT.length() - strValue.length();
        String rs = "";
        boolean isZero = false;
        for (; i < strValue.length(); i++, j++) {
            char ch = strValue.charAt(i);
            if (ch == '0') {
                isZero = true;
                if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
                    rs = rs + UNIT.charAt(j);
                    isZero = false;
                }
            } else {
                if (isZero) {
                    rs = rs + "零";
                    isZero = false;
                }
                rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
            }
        }
        if (!rs.endsWith("分") && !rs.endsWith("角")) {
            rs = rs + "整";
        }
        rs = rs.replaceAll("亿万", "亿");
        return rs;
    }


    public static void main(String[] args) {
        String s = bigDecimalToString(new BigDecimal("10234232.00"));
        System.out.println("s = " + s);
    }
}


版权声明:本文为Hmj050117原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。