中文數字轉換成阿拉伯數字 – JAVA編程語言程序開發技術文章

package cn.edu.bjfu.test;


public class Divert {


    /**
     * 隻支持千萬億級別,絕對能夠滿足日常需求
     *
     * @param src
     * @return
     */
    public static double pert(String src) {
        String temp = src.replaceAll(“整”, “”).split(“圓”,2)[0];
        int index1 = src.indexOf(“角”);
        int index2 = src.indexOf(“分”);
        String[] srcs1 = temp.split(“億”);
        long ret = 0;
        if(srcs1.length == 1) {
            if(temp.contains(“億”)) {
                ret =  fromDBK2Long(temp) * 100000000;
            } else {
                ret = fromDBK2Long(temp);
            }
        } else if(srcs1.length == 2) {
            ret = fromDBK2Long(srcs1[0]) * 100000000 + fromDBK2Long(srcs1[1]);
        }


       
        StringBuilder sb = new StringBuilder();
        sb.append(ret);
        if (index1 > -1)
            sb.append(“.”)
                    .append(Divert.getInt(String
                            .valueOf(src.charAt(index1 – 1))));
        if (index2 > -1) {
            if (index1 > -1)
                sb.append(Divert.getInt(String
                        .valueOf(src.charAt(index2 – 1))));
            else
                sb.append(“.”)
                        .append(0)
                        .append(Divert.getInt(String
                                .valueOf(src.charAt(index2 – 1))));
        }


        return Double.parseDouble(sb.toString());
    }


    private static long fromDBK2Long(String src) {
        String[] srcs = src.split(“萬”,2);
        long ret = 0;
       
        if(srcs.length == 1) {
            if(src.contains(“萬”)) {
                String[] temp = srcs[0].split(“零”);
                for(String tmp : temp)
                ret += Divert.doSomething(tmp);
               
                ret *= 10000;
            } else {
                String[] temp = srcs[0].split(“零”);
                for(String tmp : temp)
                ret += Divert.doSomething(tmp);
            }
        } else if(srcs.length == 2) {
            String[] temp1 = srcs[0].split(“零”);
            String[] temp2 = srcs[1].split(“零”);
            for(String tmp : temp1)
            ret += Divert.doSomething(tmp);
           
            ret *= 10000;
           
            for(String tmp : temp2)
            ret += Divert.doSomething(tmp);


        } else {
           
        }
       
        return ret;
    }


    private static long doSomething(String src) {
        if (src.length() == 1)
            return Divert.getInt(src);
        else if (src.length() == 2) {
            return Divert.getInt(String.valueOf(src
                    .charAt(0)))
                    * Divert.getInt(String.valueOf(src
                            .charAt(1)));
        } else if (src.length() > 2) {
            if (Divert.getInt(String.valueOf(src
                    .charAt(2))) < 10000) {


                return Divert
                .doSomething(src.substring(
                     &nbsp

發佈留言