Java實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文大寫(xiě)數(shù)字,以及中文大寫(xiě)數(shù)字到阿拉伯?dāng)?shù)字的轉(zhuǎn)換。

學(xué)習(xí)王曉華老師的《算法的樂(lè)趣》一書(shū)中第四章之后,用Java重寫(xiě)并實(shí)現(xiàn)這一功能。[圖片上傳失敗...(image-4ad7f4-1545788376630)]

該文章是在學(xué)習(xí)完該章之后的一個(gè)學(xué)習(xí)總結(jié),以供自己復(fù)習(xí)使用。

由于現(xiàn)在剛開(kāi)始學(xué)習(xí)Java,為了熟悉Java的變成規(guī)范,因此,用Java實(shí)現(xiàn)這一功能。

public class NumberToChn {
    static String CHN_NUMBER[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    static String CHN_UNIT[] = {"", "十", "百", "千"};          //權(quán)位
    static String CHN_UNIT_SECTION[] = {"", "萬(wàn)", "億", "萬(wàn)億"}; //節(jié)權(quán)位
 
    /**
     * 測(cè)試數(shù)據(jù)的數(shù)據(jù)類型
     */
    public static class Test_Data{
        int number;
        String chnNum;
        public Test_Data(int number,String chnNum){
            this.chnNum=chnNum;
            this.number=number;
        }
    }
 
    /**
     * 測(cè)試數(shù)據(jù)
     */
    static Test_Data testData[]={
            new Test_Data(0,"零"),
            new Test_Data(1,"一"),
            new Test_Data(2,"二"),
            new Test_Data(3, "三"),
            new Test_Data(4, "四"),
            new Test_Data(5, "五"),
            new Test_Data(6, "六"),
            new Test_Data(7, "七"),
            new Test_Data(8, "八"),
            new Test_Data(9, "九"),
            new Test_Data(10, "一十"),
            new Test_Data(11, "一十一"),
            new Test_Data(110, "一百一十"),
            new Test_Data(111, "一百一十一"),
            new Test_Data(100, "一百"),
            new Test_Data(102, "一百零二"),
            new Test_Data(1020, "一千零二十"),
            new Test_Data(1001, "一千零一"),
            new Test_Data(1015, "一千零一十五"),
            new Test_Data(1000, "一千"),
            new Test_Data(10000, "一萬(wàn)"),
            new Test_Data(20010, "二萬(wàn)零一十"),
            new Test_Data(20001, "二萬(wàn)零一"),
            new Test_Data(100000, "一十萬(wàn)"),
            new Test_Data(1000000, "一百萬(wàn)"),
            new Test_Data(10000000, "一千萬(wàn)"),
            new Test_Data(100000000, "一億"),
            new Test_Data(1000000000, "一十億"),
            new Test_Data(1000001000, "一十億一千"),
            new Test_Data(1000000100, "一十億零一百"),
            new Test_Data(200010, "二十萬(wàn)零一十"),
            new Test_Data(2000105, "二百萬(wàn)零一百零五"),
            new Test_Data(20001007, "二千萬(wàn)一千零七"),
            new Test_Data(2000100190, "二十億零一十萬(wàn)零一百九十"),
            new Test_Data(1040010000, "一十億四千零一萬(wàn)"),
            new Test_Data(200012301, "二億零一萬(wàn)二千三百零一"),
            new Test_Data(2005010010, "二十億零五百零一萬(wàn)零一十")
//            new Test_Data(4009060200, "四十億零九百零六萬(wàn)零二百"),
//            new Test_Data(4294967295, "四十二億九千四百九十六萬(wàn)七千二百九十五")
 
 
    };
 
    /**
     * 阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文數(shù)字的核心算法實(shí)現(xiàn)。
     * @param num為需要轉(zhuǎn)換為中文數(shù)字的阿拉伯?dāng)?shù)字,是無(wú)符號(hào)的整形數(shù)
     * @return
     */
    public static String NumberToChn(int num) {
        StringBuffer returnStr = new StringBuffer();
        Boolean needZero = false;
        int pos=0;           //節(jié)權(quán)位的位置
        if(num==0){
            //如果num為0,進(jìn)行特殊處理。
            returnStr.insert(0,CHN_NUMBER[0]);
        }
        while (num > 0) {
            int section = num % 10000;
            if (needZero) {
                returnStr.insert(0, CHN_NUMBER[0]);
            }
            String sectionToChn = SectionNumToChn(section);
            //判斷是否需要節(jié)權(quán)位
            sectionToChn += (section != 0) ? CHN_UNIT_SECTION[pos] : CHN_UNIT_SECTION[0];
            returnStr.insert(0, sectionToChn);
            needZero = ((section < 1000 && section > 0) ? true : false); //判斷section中的千位上是不是為零,若為零應(yīng)該添加一個(gè)零。
            pos++;
            num = num / 10000;
        }
        return returnStr.toString();
    }
 
    /**
     * 將四位的section轉(zhuǎn)換為中文數(shù)字
     * @param section
     * @return
     */
    public static String SectionNumToChn(int section) {
        StringBuffer returnStr = new StringBuffer();
        int unitPos = 0;       //節(jié)權(quán)位的位置編號(hào),0-3依次為個(gè)十百千;
 
        Boolean zero = true;
        while (section > 0) {
 
            int v = (section % 10);
            if (v == 0) {
                if ((section == 0) || !zero) {
                    zero = true; /*需要補(bǔ)0,zero的作用是確保對(duì)連續(xù)的多個(gè)0,只補(bǔ)一個(gè)中文零*/
                    //chnStr.insert(0, chnNumChar[v]);
                    returnStr.insert(0, CHN_NUMBER[v]);
                }
            } else {
                zero = false; //至少有一個(gè)數(shù)字不是0
                StringBuffer tempStr = new StringBuffer(CHN_NUMBER[v]);//數(shù)字v所對(duì)應(yīng)的中文數(shù)字
                tempStr.append(CHN_UNIT[unitPos]);  //數(shù)字v所對(duì)應(yīng)的中文權(quán)位
                returnStr.insert(0, tempStr);
            }
            unitPos++; //移位
            section = section / 10;
        }
        return returnStr.toString();
    }
 
    /**
     * 完成將阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文數(shù)字的測(cè)試
     */
    public static void TestNumToChn(){
        for(int i=0;i<testData.length;i++) {
            String str=NumberToChn(testData[i].number);
            System.out.println(testData[i].number+"\t"+testData[i].chnNum+"\t"+str+"\t"+str.equals(testData[i].chnNum));
        }
    }
 
 
    /**
     * 中文轉(zhuǎn)換成阿拉伯?dāng)?shù)字,中文字符串除了包括0-9的中文漢字,還包括十,百,千,萬(wàn)等權(quán)位。
     * 此處是完成對(duì)這些權(quán)位的類型定義。
     * name是指這些權(quán)位的漢字字符串。
     * value是指權(quán)位多對(duì)應(yīng)的數(shù)值的大小。諸如:十對(duì)應(yīng)的值的大小為10,百對(duì)應(yīng)為100等
     * secUnit若為true,代表該權(quán)位為節(jié)權(quán)位,即萬(wàn),億,萬(wàn)億等
     */
    public static class Chn_Name_value{
        String name;
        int value;
        Boolean secUnit;
        public Chn_Name_value(String name,int value,Boolean secUnit){
            this.name=name;
            this.value=value;
            this.secUnit=secUnit;
        }
    }
 
    static Chn_Name_value chnNameValue[]={
            new Chn_Name_value("十",10,false),
            new Chn_Name_value("百",100,false),
            new Chn_Name_value("千",1000,false),
            new Chn_Name_value("萬(wàn)",10000,true),
            new Chn_Name_value("億",100000000,true)
    };
 
    /**
     * 返回中文數(shù)字漢字所對(duì)應(yīng)的阿拉伯?dāng)?shù)字,若str不為中文數(shù)字,則返回-1
     * @param str
     * @return
     */
    public static int ChnNumToValue(String str){
        for(int i=0;i<CHN_NUMBER.length;i++){
            if(str.equals(CHN_NUMBER[i])){
                return i;
            }
        }
        return -1;
    }
 
    /**
     * 返回中文漢字權(quán)位在chnNameValue數(shù)組中所對(duì)應(yīng)的索引號(hào),若不為中文漢字權(quán)位,則返回-1
     * @param str
     * @return
     */
    public static int ChnUnitToValue(String str){
        for(int i=0;i<chnNameValue.length;i++){
            if(str.equals(chnNameValue[i].name)){
                return i;
            }
        }
        return -1;
    }
 
    /**
     * 返回中文數(shù)字字符串所對(duì)應(yīng)的int類型的阿拉伯?dāng)?shù)字
     * @param str
     * @return
     */
    public static int ChnStringToNumber(String str){
        int returnNumber=0;
        int section=0;
        int pos=0;
        int number=0;
        while (pos<str.length()){
            int num=ChnNumToValue(str.substring(pos,pos+1));
            //若num>=0,代表該位置(pos),所對(duì)應(yīng)的是數(shù)字不是權(quán)位。若小于0,則表示為權(quán)位
            if(num>=0){
                number=num;
                pos++;
                //pos是最好一位,直接將number加入到section中。
                if(pos>=str.length()){
                    section+=number;
                    returnNumber+=section;
                    break;
                }
            }else{
                int chnNameValueIndex=ChnUnitToValue(str.substring(pos,pos+1));
                //chnNameValue[chnNameValueIndex].secUnit==true,表示該位置所對(duì)應(yīng)的權(quán)位是節(jié)權(quán)位,
                if(chnNameValue[chnNameValueIndex].secUnit){
                    section=(section+number)*chnNameValue[chnNameValueIndex].value;
                    returnNumber+=section;
                    section=0;
                }else{
                    section+=number*chnNameValue[chnNameValueIndex].value;
                }
                pos++;
                number=0;
                if(pos>=str.length()){
                    returnNumber+=section;
                    break;
                }
            }
        }
        return returnNumber;
    }
 
    /**
     * 完成對(duì)中文數(shù)字字符串轉(zhuǎn)換成阿拉伯?dāng)?shù)字函數(shù)的測(cè)試
     */
    public static void TestChnStringToNumber(){
        for(int i=0;i<testData.length;i++){
            int number=ChnStringToNumber(testData[i].chnNum);
            System.out.println(testData[i].chnNum+"\t"+number+"\t"+testData[i].number+"\t"+(number==testData[i].number));
 
        }
    }
 
    public static void main(String[] args) {
        TestNumToChn();
        System.out.println("--------------------------------------------------------");
        TestChnStringToNumber();
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容