Java實現將阿拉伯數字轉換爲中文數字123=》一二三

package day1;

import java.util.ArrayList;
import java.util.Collections;

public class test {
    
     /** 
     * 單位進位,中文默認爲4位即(萬、億) 
     */  
    public int UNIT_STEP = 4;  
  
    /** 
     * 單位 
     */  
    public String[] CN_UNITS = new String[] { "個", "十", "百", "千", "萬", "十",  
            "百", "千", "億", "十", "百", "千", "萬" };  
  
    /** 
     * 漢字 
     */  
    public String[] CN_CHARS = new String[] { "零", "一", "二", "三", "四",  
            "五", "六", "七", "八", "九" };  
  
    /** 
     * 數值轉換爲中文字符串 
     *  
     * @param num 
     * 須要轉換的數值 
     * @return 
     */  
    public String cvt(long num) {  
        return this.cvt(num, false);  
    }  
  
    /** 
     * 數值轉換爲中文字符串(口語化) 
     *  
     * @param num 
     *            須要轉換的數值 
     * @param isColloquial 
     *            是否口語化。例如12轉換爲'十二'而不是'一十二'。 
     * @return 
     */  
    public String cvt(long num, boolean isColloquial) {  
        String[] result = this.convert(num, isColloquial);  
        StringBuffer strs = new StringBuffer(32);  
        for (String str : result) {  
            strs.append(str);  
        }  
        return strs.toString();  
    }  
  
    /** 
     * 將數值轉換爲中文 
     *  
     * @param num 
     *            須要轉換的數值 
     * @param isColloquial 
     *            是否口語化。例如12轉換爲'十二'而不是'一十二'。 
     * @return 
     */  
    public String[] convert(long num, boolean isColloquial) {  
        if (num < 10) {// 10如下直接返回對應漢字  
            return new String[] { CN_CHARS[(int) num] };// ASCII2int  
        }  
  
        char[] chars = String.valueOf(num).toCharArray();  
        if (chars.length > CN_UNITS.length) {// 超過單位表示範圍的返回空  
            return new String[] {};  
        }  
  
        boolean isLastUnitStep = false;// 記錄上次單位進位  
        ArrayList<String> cnchars = new ArrayList<String>(chars.length * 2);// 建立數組,將數字填入單位對應的位置  
        for (int pos = chars.length - 1; pos >= 0; pos--) {// 從低位向高位循環  
            char ch = chars[pos];  
            String cnChar = CN_CHARS[ch - '0'];// ascii2int 漢字  
            int unitPos = chars.length - pos - 1;// 對應的單位座標  
            String cnUnit = CN_UNITS[unitPos];// 單位  
            boolean isZero = (ch == '0');// 是否爲0  
            boolean isZeroLow = (pos + 1 < chars.length && chars[pos + 1] == '0');// 是否低位爲0  
  
            boolean isUnitStep = (unitPos >= UNIT_STEP && (unitPos % UNIT_STEP == 0));// 當前位是否須要單位進位  
  
            if (isUnitStep && isLastUnitStep) {// 去除相鄰的上一個單位進位  
                int size = cnchars.size();  
                cnchars.remove(size - 1);  
                if (!CN_CHARS[0].equals(cnchars.get(size - 2))) {// 補0  
                    cnchars.add(CN_CHARS[0]);  
                }  
            }  
  
            if (isUnitStep || !isZero) {// 單位進位(萬、億),或者非0時加上單位  
                cnchars.add(cnUnit);  
                isLastUnitStep = isUnitStep;  
            }  
            if (isZero && (isZeroLow || isUnitStep)) {// 當前位爲0低位爲0,或者當前位爲0而且爲單位進位時進行省略  
                continue;  
            }  
            cnchars.add(cnChar);  
            isLastUnitStep = false;  
        }  
  
        Collections.reverse(cnchars);  
        // 清除最後一位的0  
        int chSize = cnchars.size();  
        String chEnd = cnchars.get(chSize - 1);  
        if (CN_CHARS[0].equals(chEnd) || CN_UNITS[0].equals(chEnd)) {  
            cnchars.remove(chSize - 1);  
        }  
  
        // 口語化處理  
        if (isColloquial) {  
            String chFirst = cnchars.get(0);  
            String chSecond = cnchars.get(1);  
            if (chFirst.equals(CN_CHARS[1]) && chSecond.startsWith(CN_UNITS[1])) {// 是否以'一'開頭,緊跟'十'  
                cnchars.remove(0);  
            }  
        }  
        return cnchars.toArray(new String[] {});  
    }  
  
    public static void main(String[] args) {  
        String[] testData = ("0,1,2,3,4,5,6,7,8,9,"  
                + "10,11,20,91,"  
                + "110,102,1234,1023,1002,"  
                + "10000,12345,10234,10023,10002,12034,12003,12000,12300,12340,"  
                + "100000,123456,102345,200234,100023,100002,120345,120034,120003,120000,123000,123400,123450,"  
                + "1000000,1234567,2023456,1002345,3000234,1000023,1000002,1203456,1200345,1200034,1200003,1200000,1230000,1234000,1234500,1234560,"  
                + "10000000,12345678,10234567,10023456,10002345,10000234,10000023,12034567,12003456,12000345,12000034,12000003,12000000,12300000,12340000,12345600,12345670,"  
                + "1000000000,1234567890,1023456789,1002300078,1000230067,1000023456,1000002345,1203456789,1200345678,1200034567,1200003456,1200000345,1200000034,1200000003,1230000000,1234000000,1234500000,1234560000,1234567000,1234567800,1234567890,"  
  
                + String.valueOf(Integer.MAX_VALUE)  
  
        + ",9223372036854,9002233007200").split(",");  
        for (String data : testData) {  
            long num = Long.parseLong(data);  
  
            if (num < 0) {  
                continue;  
            }  
  
            System.out.println(String.format("%-12s \t %s", data,  
                    new test().cvt(num,true)));  
  
        }  
  
    }  
        
}
相關文章
相關標籤/搜索