java中浮点数转人民币的实现方法

来源:爱站网时间:2019-08-17编辑:网友分享
java如何实现浮点数转人民币?对于此问题是不是很多人都会好奇呢?下面爱站小编就用java实现浮点数转人民币的小例子为大家详细介绍java中浮点数转人民币的实现方法。

java如何实现浮点数转人民币?对于此问题是不是很多人都会好奇呢?下面爱站小编就用java实现浮点数转人民币的小例子为大家详细介绍java中浮点数转人民币的实现方法。

 

import java.util.ArrayList;
import java.util.List;

public class RMBConverter2 implements IRMBConverter {

    private static final String [] RMB_NUMBER ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

    private static final String [] RMB_ATTR = {"元","十","佰","仟","万","十","百","仟","亿","十","佰","仟","万","十","佰","仟"};

    private static final String[] RMB_FEN = {"分","角"};

     
    @Override
    public String change2RMB(double counts) {
        StringBuffer rsBuf = new StringBuffer();

        if (counts == 0L) {
            return "零";
        }

        if (counts < 0) 
            rsBuf.append("负");

        String zheng = strConverse(String.valueOf((long)counts));
        String xiao = strConverse(String.valueOf(Math.round((counts - (long)counts)*100)));

        char [] zhengArray = zheng.toCharArray();
        char [] xiaoArray = xiao.toCharArray();

        int zLength = zhengArray.length;
        int xLength = xiaoArray.length;

        List<String> zhengList = new ArrayList<String>();

        for(int i =0; i< zLength; i++) {
            int value = Integer.parseInt(""+zhengArray[i]);
            if (value == 0 && (i != 4) && (i != 8) && i != 0) {
                zhengList.add(RMB_NUMBER[value]);
            }else {
                zhengList.add(RMB_NUMBER[value]+
                     RMB_ATTR[i]);
            }
        }

        for(int i = zhengList.size()-1; i>=0; i--) {
            rsBuf.append(zhengList.get(i));
        }

         
        for(int i =0; i<xLength ;i++) {
            int value = Integer.parseInt(""+xiaoArray[i]);
            if (value > 0 ) {
                rsBuf.append(RMB_NUMBER[value])
                 .append(RMB_FEN[xLength-i-1]);
            }
        }

        String rs = rsBuf.toString();
        rs = rs.replaceAll("零+", "零");
        rs = rs.replaceAll("零元", "元");
        rs = rs.replaceAll("零万", "万");
        rs = rs.replaceAll("零亿", "亿");
        rs = rs.replaceAll("亿万", "亿");
        return rs;
    }

     
    private String strConverse(String str) {
        StringBuffer rs = new StringBuffer();
        char [] strArray = str.toCharArray();
        for(int i = str.length()-1; i>=0; i--) {
            rs.append(strArray[i]);
        }
        return rs.toString();
    }

     
    public static void main(String args []) {

        RMBConverter2 c2 = new RMBConverter2();
        System.out.println(c2.change2RMB(2001004.10));
        System.out.println(c2.change2RMB(2000000.10));
        System.out.println(c2.change2RMB(0L));
        System.out.println(c2.change2RMB(1121000010000000L));
    }

}

 

 

上文介绍了java中浮点数转人民币的实现方法,还不了解的小伙伴们赶紧来参考一下吧,觉得有帮助就请关注我们吧。

上一篇:Java进制转换

下一篇:关于JAVA Stack

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载