阿拉伯数字转换成英文以及中文表达

题目描述

把一串数字装换成英文表达或者中文表达。
例如:1234转换成一千二百三十四和one southand two hundred thirty four

思路分析

此题就是纯coding问题,不过,需要注意一个特殊技巧,那就是,类似于职责链模式一样的代码编写格式,我们先写个位,个位的结果就那几个,需要枚举出来,然后,处理十位上的数,此时,我们可以把十位单独提出来处理后,剩下的调用处理个位的函数。同理,处理百位时,也是如此,我们并不需要从头到尾重复处理,因为十位个位已经有相关函数处理了,此时,只需要处理百位后,再调用十位的函数,十位又会调用个位,类似于职责链模式的代码。
需要注意的是,英语中,三位是一体的,Thousand、Million、Billion。而中文中,4位是一体的,依次是万和亿,拼接上基础的个十百千来搞定。

转英文代码

    public static String toEnglish(int num){
        String str="";
        if(num<0){
            str+="negative ";
            num=-num;
        }
        if(num==0){
            return "zero";
        }
        if(num<1000){
            return e0to999(num);
        }

        while (num>1000){
            if(num>1000000000){
                str+=toEnglish(num/1000000000)+" Billion ";
                num=num%1000000000;
            }
            if(num>1000000){
                str+=toEnglish(num/1000000)+" Million ";
                num=num%1000000;
            }
            if(num>1000){
                str+=toEnglish(num/1000)+" Thousand ";
                num=num%1000;
            }
        }
        str+=e0to999(num);
        return str;
    }

    public static String e0to19(int num){
        String[] str=new String[]{"one","two","three","four","five","six","seven","eight","nine","ten",
        "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","nineteen"};
        return str[num-1]+" ";
    }

    public static String e0to99(int num){
        if(num<20){
            return e0to19(num);
        }
        String[] str={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        return str[num/10-2]+" "+e0to19(num%10);
    }

    public static String e0to999(int num){
        if(num<100){
            return e0to99(num);
        }
        return e0to19(num/100)+" hundred "+e0to99(num%100);
    }

转中文代码

    public static String toChinese(int num){
        String str="";
        if(num<0){
            str+="负";
            num=-num;
        }
        if(num==0){
            return "零";
        }
        if(num<10000){
            return c0to9999(num);
        }
        while (num>10000){
            if(num>100000000){
                str+=toChinese(num/100000000)+"亿";
                num=num%100000000;
            }
            if(num>10000){
                str+=toChinese(num/10000)+"万";
                num=num%10000;
            }
        }
        str+=c0to9999(num);
        return str;
    }

    public static String c0to9(int num){
        String[] str={"一","二","三","四","五","六","七","八","九"};
        return str[num-1];
    }

    public static String c0to99(int num){
        if(num<9){
            return c0to9(num);
        }
        String[] str={"十","二十","三十","四十","五十","六十","七十","八十","九十"};
        return str[num/10-1]+c0to9(num%10);
    }

    public static String c0to999(int num){
        if(num<99){
            return c0to99(num);
        }
        String[] str={"一百","二百","三百","四百","五百","六百","七百","八百","九百"};
        if(num%100<10){
            return str[num/100-1]+"零"+c0to9(num%100);
        }else {
            return str[num/100-1]+c0to99(num%100);
        }
    }

    public static String c0to9999(int num){
        if(num<999){
            return c0to999(num);
        }
        String[] str={"一千","二千","三千","四千","五千","六千","七千","八千","九千"};
        if(num%1000<100){
            return str[num/1000-1]+"零"+c0to99(num%1000);
        }else {
            return str[num/1000-1]+c0to999(num%1000);
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值