[2019.01.11] algorithm learning record - Arabic numeral turn Chinese money (to be improved)

Algorithms - Arabic numeral turn Chinese money


Enter the Arabic numerals, the Chinese conversion amount is limited to less than one hundred million numbers.
The title comes from the network

public class Test {
    public static void main(String[] args) {
        System.out.println(outputMoney(3487031));
    }

    public static String exchangeNumber(String num) {
        String result = "";
        switch (num) {
            case "0":
                result = "零";
                break;
            case "1":
                result = "壹";
                break;
            case "2":
                result = "贰";
                break;
            case "3":
                result = "叁";
                break;
            case "4":
                result = "肆";
                break;
            case "5":
                result = "伍";
                break;
            case "6":
                result = "陆";
                break;
            case "7":
                result = "柒";
                break;
            case "8":
                result = "捌";
                break;
            case "9":
                result = "玖";
                break;
        }
        return result;
    }

    public static String danWeiExchange(int i) {
        String[] danwei = new String[4];
        danwei[0] = "元";
        danwei[1] = "十";
        danwei[2] = "百";
        danwei[3] = "千";
        return danwei[i];
    }

    public static String danWeiExchange2(int i) {
        String[] danwei = new String[4];
        danwei[0] = "万";
        danwei[1] = "十";
        danwei[2] = "百";
        danwei[3] = "千";
        return danwei[i];
    }

    public static String outputMoney(int number) {
        String result = "";
        if(number<10000){
            String valueQ = String.valueOf(number);
            char[] arrQ = valueQ.toCharArray();
            int weiQ = arrQ.length;
            for(int i=0; i< weiQ; i++){
                if(arrQ[i] != '0'){
                    result += exchangeNumber(String.valueOf(arrQ[i]))+danWeiExchange(weiQ-i-1);
                }
            }
        }
        if(10000<=number && number<100000000){
            String resultXiao = "";
            String resultDa = "";
            int numberXiao = number-number/10000*10000;
            int numberDa = number/10000;
            String valueXiao = String.valueOf(numberXiao);
            String valueDa = String.valueOf(numberDa);
            char[] arrXiao = valueXiao.toCharArray();
            int weiXiao = arrXiao.length;
            for(int i = 0; i<weiXiao;i++){
                if(arrXiao[i] != '0'){
                    resultXiao += exchangeNumber(String.valueOf(arrXiao[i])) + danWeiExchange(weiXiao -i-1);
                }
            }
            char[] arrDa = valueDa.toCharArray();
            int weiDa = arrDa.length;
            for(int i = 0; i< weiDa; i++){
                if(arrDa[i] != '0'){
                    resultDa += exchangeNumber(String.valueOf(arrDa[i])) + danWeiExchange2(weiDa-i-1);
                }
            }
            result = resultDa + resultXiao;
        }
        return result;

    }
}

Published 17 original articles · won praise 0 · Views 330

Guess you like

Origin blog.csdn.net/cletitia/article/details/103935307