JAVA版数字转人民币大写-四位数分隔法

本文介绍了作者为解决金额转换问题,通过四位分割法和DecimalFormat,创建了一个Java程序,将数字金额精确转换为中文大写形式,并与WPS Excel进行了对比验证。文中还提及了WPS转换金额的局限性。

  一、背景

          今天在网络上找金额数字转人民币大写,找了几个版本,下载使用后都不怎么理想,或多或少存在点问题,另外一个原因就是出现了更大的金额兆(万亿),最后在忍无可忍的情况下决定自己动手写金额转换的程序。


  二、基本思路四位分割方法

        因为中文的数字表达体系是“个、十、百、千、万、十万、百万、千万、亿、十亿、百亿、千亿、兆、十兆、百兆、千兆、京、十京、百京、千京、垓、十垓、百垓……”,每四位数升一个级。例如:

                123456789 --->   1+  单位:亿+ (一个轮回:2仟3佰4拾5) +单位万 +(一个轮回:6仟7佰8拾9)元

        采用这种方法,我们就可以JAVA的DecimalFormat 表达式,将上述的金额进行格式化,然后就可以4位一个单元进行金额转换,转换好后再加上相应的单位(万,亿,兆,京),就大功告成了。测试后的结果和WPS的EXCEL进行了对比,测试案例中均保持了一致,有问题欢迎指正。  

三、JAVA版本代码



import java.math.BigDecimal;
import java.text.DecimalFormat;

public class ToChinseMoney {

    private static String[] unit = {"", "万", "亿", "兆", "京"};
    private static String[] beforeScale = {"仟", "佰", "拾", ""};
    private static String[] numArray = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] afterScale = {"角", "分"};

    public static String toUpperCaseZhCn(BigDecimal value) {
        StringBuffer buf = new StringBuffer();
        if (value.compareTo(new BigDecimal("0")) < 0)
            buf.append("负");
        value = value.abs();
        String c[] = value.toString().split("\\.");//分割出来,.前面对应角分
        splitByFour(c[0], buf);
       
        if (c.length > 1 && (c[1].toCharArray()[0] - 48 != 0 || c[1].toCharArray()[1] - 48 != 0)) {
            char[] decimal = c[1].toCharArray();
            if (decimal[0] - 48 == 0)
                buf.append(numArray[decimal[0] - 48]);
            else
                buf.append(numArray[decimal[0] - 48]).append(afterScale[0]);
            if (decimal.length > 1 && decimal[1] - 48 != 0)
                buf.append(numArray[decimal[1] - 48]).append(afterScale[1]);
        } 

        return buf.toString();
    }

    public static void splitByFour(String strIntpart, StringBuffer buf) {
          if(new BigDecimal("0").equals(  new BigDecimal(strIntpart)))
            {
            buf.append("零");
            return;
            }

        DecimalFormat df4 = new DecimalFormat("####,####,####,####,####");
        String formatMount = df4.format(new BigDecimal(strIntpart));
        String[] splitList = formatMount.split(",");
        if (splitList.length > 5) {
            System.out.println("金额超限!");
//                throw new Exception("金额超限!");
        }
        for (int n = 0; n < splitList.length; n++) {
            String onePart = splitList[n];
            if (Integer.parseInt(onePart) != 0){
                RecursionChangeTo(onePart, buf, false);
                buf.append(unit[splitList.length - 1 - n]);
            }
        }

    }

    public static void RecursionChangeTo(String strIntpart, StringBuffer buf, boolean preIsZero) {
        BigDecimal Intpart = new BigDecimal(strIntpart);
        boolean iszero = false;

        String topone = "";
        if (strIntpart.length() > 1)
            topone = strIntpart.substring(0, 1);
        else
            topone = strIntpart;
        int inttopone = Integer.parseInt(topone);

        if (inttopone != 0) {
            if (preIsZero)//前面是0 ,这里不是0
                buf.append("零");
            //金额部分
            buf.append(numArray[inttopone]);
            //单位部分
            buf.append(beforeScale[beforeScale.length - strIntpart.length()]);
        } else
            iszero = true;

        if (strIntpart.length() > 1) {
            String nextString = strIntpart.substring(1, strIntpart.length());
            if (inttopone != 0)
                RecursionChangeTo(nextString, buf, false);
            else
                RecursionChangeTo(nextString, buf, true);
        }
    }

    

}

三、结果验证

    这里采用了WPS的EXCEL对数字转人民币大写做了验证,在验证过程中也发现了WPS转换的最高限制,见图片黄色部分:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值