JAVA版数字转人民币大写-四位分割法(支持京兆)

Posted 火柴盒zhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA版数字转人民币大写-四位分割法(支持京兆)相关的知识,希望对你有一定的参考价值。

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


        网络上找的版本在处理小数点前面部分基本都是一个整体考虑,这次自己写的版本则采用4位一个分割的方式处理,测试后的结果和WPS的EXCEL进行了对比,测试案例中都保持了一致,草稿版本,有问题欢迎指正。

        测试过程中也发现了WPS的EXCEL对数字转人民币大写的最高限制,见图片黄色部分:


  

代码如下:



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);
        
    

    public static void main(String[] args) 


        String  number = "1";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "12";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "20";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "99";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "111";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "100";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "300";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "303";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "999";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "4444";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "4000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "4400";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "4440";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "4040";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "4004";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "55555";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "50000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "55000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "55500";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "55550";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "50500";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "50505";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "50050";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "50005";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "600000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "660000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "666000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "666600";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "666660";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "606000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "600600";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "600060";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "600006";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "666666";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7700000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7770000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7777000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7777700";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7777770";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "7777777";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7070000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7070700";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7070707";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "7007000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7000700";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7000070";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "7000007";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "80000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88800000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88880000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88888000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88888800";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88888880";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "88888888";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80800000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80080000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80008000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80000800";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80000080";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "80000008";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "900000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "990000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999900000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999900000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999990000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999999000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999999900";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999999990";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "999999999";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "909000000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900900000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900090000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900009000";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900000900";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900000090";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "900000009";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "909090909";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "909009999";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));




        number = "1000000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));



        number = "100000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100000000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "10000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "1000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "100000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "10000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100000000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "10000000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "1000000000000";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "100000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "10000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1000000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "10000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1000001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "100001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "100001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "10001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "1001";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "101";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1234567890123456789";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "123456789012345678";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "12345678901234567";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1234567890123456";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "123456789012345";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "12345678901234";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "1234567890123";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "123456789012";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));



        number = "1234567890";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "123456789";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "12345678";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "1234567";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "123456";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "12345";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "1234";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        number = "123";

        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));





        number = "102559456390605";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
//        number = "102559456390605.26";
//        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "32379920661342";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));
        number = "32379920661342";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));

        number = "-97139757337071";
        System.out.println(number + ": " + toUpperCaseZhCn(new BigDecimal(number)));


        System.out.println("116073:" + toUpperCaseZhCn(new BigDecimal("116073")));
        System.out.println("100:" + toUpperCaseZhCn(new BigDecimal("100")));
        //一亿零八十八万三千四百三十圆零一分
        System.out.println("100883430:" + toUpperCaseZhCn(new BigDecimal("100883430")));
        //一百零肆亿四千零二十万零二百圆零一分
        System.out.println("10440200200:" + toUpperCaseZhCn(new BigDecimal("10440200200")));
        //一百零肆亿四千零伍十万零二百圆
        System.out.println("10440500200:" + toUpperCaseZhCn(new BigDecimal("10440500200")));
        System.out.println("10000000000:" + toUpperCaseZhCn(new BigDecimal("10000000000")));
        System.out.println("100000000:" + toUpperCaseZhCn(new BigDecimal("100000000")));
        System.out.println("201600:" + toUpperCaseZhCn(new BigDecimal("201600")));
        System.out.println("47630000:" + toUpperCaseZhCn(new BigDecimal("47630000")));
        System.out.println("50860000.:" + toUpperCaseZhCn(new BigDecimal("50860000")));
    



以上是关于JAVA版数字转人民币大写-四位分割法(支持京兆)的主要内容,如果未能解决你的问题,请参考以下文章

java 数字转大写汉字

小写数字自动转换成大写人民币金额两张表怎么在桌面找不同吗?

Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)

excel金额如何转为大写?

Java将控制台输入的人民币数字金额转化为大写

在WORD中怎样用VBA将人民币大写金额转换成小写金额