close

    //將字串所有的國字小數轉阿拉伯小數(需搭配判斷function 在另一篇文章)
    private String chPointToPoint(String pointString) {
        while (isPointNumeric(pointString)) {//有小數點
            String chpoint = getChPointMath(pointString);//找出第一組小數點
            String[] chpointArray = chpoint.split("點");//用點拆成兩個數字
            chpointArray[0] =  StrToInt(chpointArray[0])+"";//轉成數字
            chpointArray[1] =  StrToInt(chpointArray[1])+"";//轉成數字
            String newString = chpointArray[0]+"."+chpointArray[1];//組合成新的小數點數字
            pointString = pointString.replace(chpoint, newString);//取代掉原本的國字小數
        }
        return pointString;
    }    

//找出字串中第一組阿拉伯數字(對應intToStr方法使用)
    private String getMath(String value) {
        String math = "0123456789";
        String str = "";
        for(int index = 0 ; index < value.length() ; index++) {
            if(math.contains(value.substring(index,index+1))) {
                str+=value.substring(index,index+1);
            }else if(str.length()>0) {
                return str;
            }
        }
        return str;
    }
    //找出字串中第一組國字數字(對應StrToInt方法使用)
    private String getChMath(String value) {
        String ch = "零一二三四五六七八九兩十百千萬億兆";
        String str = "";
        for(int index = 0 ; index < value.length() ; index++) {
            if(ch.contains(value.substring(index,index+1))) {
                str+=value.substring(index,index+1);
            }else if(str.length()>0) {
                return str;
            }
        }
        //單純只有單位的話 不算一組國字數字
        if(str.equalsIgnoreCase("十")||str.equalsIgnoreCase("百")||str.equalsIgnoreCase("千")||
                str.equalsIgnoreCase("萬")||str.equalsIgnoreCase("億")||str.equalsIgnoreCase("兆"))
            return "";
        return str;
    }    

// 找出字串中第一組國字小數數字
    private String getChPointMath(String value) {
        String ch = "零一二兩三四五六七八九兩十百千萬億兆點";
        String str = "";
        for (int index = 0; index < value.length(); index++) {
            if (ch.contains(value.substring(index, index + 1))) {
                str += value.substring(index, index + 1);
            } else if (str.length() > 0) {
                return str;
            }
        }
        // 單純只有單位的話 不算一組國字數字
        if (str.equalsIgnoreCase("十") || str.equalsIgnoreCase("百") || str.equalsIgnoreCase("千")
                || str.equalsIgnoreCase("萬") || str.equalsIgnoreCase("億") || str.equalsIgnoreCase("兆"))
            return "";
        if(!str.contains("點")||str.substring(str.length()-1,str.length()).equalsIgnoreCase("點")||str.substring(0,1).equalsIgnoreCase("點"))
            return "";
        return str;
    }

//國字轉數字
    private static long StrToInt(String value1) {
        int unitPosition1 = value1.indexOf("萬");
        int unitPosition2 = value1.indexOf("億");
        int unitPosition3 = value1.indexOf("兆");
        long mathValueTotal = 0;
        ArrayList<String> unitMath = new ArrayList();//X X萬X億X兆
        unitMath.add(value1);
        //複製if else可擴充單位unitPosition....
        if(unitPosition3>0) {
            String[] unitStr = unitMath.get(0).split("兆");
            unitMath.set(0, unitStr[1]);//將value刪除兆
            unitMath.add(1, unitStr[0]);//兆前面的數字往後塞
        }else {
            unitMath.add(1, "零");//沒有兆則塞零
        }
        if(unitPosition2>0) {
            String[] unitStr = unitMath.get(0).split("億");
            unitMath.set(0, unitStr[1]);//將value刪除億
            unitMath.add(1, unitStr[0]);//億前面的數字往後塞
        }else {
            unitMath.add(1, "零");//沒有億則塞零
        }
        if(unitPosition1>0) {
            String[] unitStr = unitMath.get(0).split("萬");
            unitMath.set(0, unitStr[1]);//將value刪除萬
            unitMath.add(1, unitStr[0]);//萬前面的數字往後塞
        }else {
            unitMath.add(1, "零");//沒有萬則塞零
        }
        
        
        for(int unitIndex = 0 ; unitIndex < unitMath.size();unitIndex++) {
            String value = unitMath.get(unitIndex);
        String ch = "零一二三四五六七八九";
        int TenPosition = value.indexOf("十");
        int HunPosition = value.indexOf("百");
        int thoPosition = value.indexOf("千");
        long mathValue = 0;
        if(thoPosition>0) {
            String Ch =  value.substring(thoPosition-1,thoPosition);//取得千位的數字
            int Ma = ch.indexOf(Ch);//千位數字轉國字
            if(Ma==-1)
                Ma = 2;//兩 
            mathValue+= Ma*1000;
        }
        if(HunPosition>0) {
            String Ch =  value.substring(HunPosition-1,HunPosition);//取得百位的數字
            int Ma = ch.indexOf(Ch);//百位數字轉國字
            if(Ma==-1)
                Ma = 2;//兩 
            mathValue+= Ma*100;
        }
        if(TenPosition>0) {
            String Ch =  value.substring(TenPosition-1,TenPosition);//取得十位的數字
            int Ma = ch.indexOf(Ch);//十位數字轉國字
            if(Ma==-1)
                Ma = 2;//兩 
            mathValue+= Ma*10;
        }
        if(TenPosition==value.length()-1||HunPosition==value.length()-1||thoPosition==value.length()-1) {

        }else {//十百千都不在最後一位則有個位數
            String Ch =  value.substring(value.length()-1,value.length());//取得個位的數字
            int Ma = ch.indexOf(Ch);//個位數字轉國字
            mathValue+= Ma*1;
        } 
        mathValueTotal += mathValue*Math.pow(10000,unitIndex);//依照位置為萬單位
        }
        // 沒有單位的轉換
        String ch = "零一二三四五六七八九";
        long mathValueTotal1 = 0;
        int powIndex = 0;
        for (int index = value1.length(); index > 0; index--) {
            int position = ch.indexOf(value1.substring(index - 1, index));
            if (position >= 0) {
                mathValueTotal1 += position * Math.pow(10, powIndex++);
            }  else if(value1.substring(index - 1, index).equalsIgnoreCase("兩")) {// 兩
                mathValueTotal1 += 2 * Math.pow(10, powIndex++);
            }
        }
        if (mathValueTotal1 > mathValueTotal)
            return mathValueTotal1;
        else
            return mathValueTotal;
    }

  

 //數字轉國字(不包含單位..十百千萬億兆)
    private static String DateintToStr(long value) {//數字需要去除零零
        String[] ch = {"零","一","二","三","四","五","六","七","八","九"};
        int One = (int)(value%10);
        int Ten = (int)(value%100/10);
        int Hun = (int)(value%1000/100);
        int tho = (int)(value%10000/1000);
        int pos = 0;
        String chStr = "";
        if(tho>0) {//千位
            chStr= ch[tho]+ch[Hun]+ch[Ten]+ch[One];
        }else if(Hun>0) {//百位
            chStr=ch[Hun]+ch[Ten]+ch[One];
        }else if(Ten>0) {//十位
            chStr=ch[Ten]+ch[One];
        }else {
            chStr=ch[One];
        }
        if(value>=10000) {
            chStr = DateintToStr(value/10000)+chStr;
        }
        return chStr;
    }
    //數字轉國字(包含單位..十百千萬億兆)
    private static String intToStr(long value,int unitInit) {//數字需要去除
        String[] ch = {"零","一","二","三","四","五","六","七","八","九"};
        String[] unit = {"","萬","億","兆"};
        int One = (int)(value%10);
        int Ten = (int)(value%100/10);
        int Hun = (int)(value%1000/100);
        int tho = (int)(value%10000/1000);
        int pos = 0;
        String chStr = ch[tho]+"千"+ch[Hun]+"百"+ch[Ten]+"十"+ch[One];
        while(chStr.subSequence(0, 1).equals("零")&& chStr.length()>1) {//去頭"零"
            chStr = chStr.substring(2, chStr.length());
        }
        if(chStr.subSequence(chStr.length()-1, chStr.length()).equals("零")&& chStr.length()>1) {
            chStr = chStr.substring(0, chStr.length()-1);
        }
        while(chStr.length()>1 && chStr.subSequence(chStr.length()-2, chStr.length()-1).equals("零")) {//去尾"零"
            chStr = chStr.substring(0, chStr.length()-2);
        }
        if(chStr.length()>1 &&chStr.subSequence(0,2).equals("一十")) {
            chStr =chStr.substring(1, chStr.length());
        }
        chStr = chStr.replace("零千", "零");
        chStr = chStr.replace("零百", "零");
        chStr = chStr.replace("零十", "零");
        chStr = chStr.replace("零零", "零");
        chStr = chStr.replace("二百", "兩百");
        chStr = chStr.replace("二千", "兩千");
        if(value>=10000) {
            chStr = intToStr(value/10000,++unitInit)+unit[unitInit]+chStr;
        }
        return chStr;
    }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JBLin 的頭像
    JBLin

    a0973378273的部落格

    JBLin 發表在 痞客邦 留言(0) 人氣()