數字轉中文【適用於金額轉換和普通數字轉換】


                            數字轉中文【適用於金額轉換和普通數字轉換】
java


做者:Vashongit

時間:20151015數組

       今天在項目裏有個地方須要將數字轉換成中文,而後寫了一個粗糙的工具類(時間緊迫),回到家裏總結了下,寫成一下兩個方法,一個是普通阿拉伯數字轉換成中文,另外一個是金額數字轉換成中文。工具


代碼分塊展現:copy常量數組,而後直接調用寫好的方法便可。測試

1、定義簡體、繁體常量數組spa

/**簡體中文形式**/
	private static final String[] numArray = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
	/**繁體中文形式**/
	private static final String[] chineseDigits = new String[] { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};

2、普通阿拉伯數字轉成中文方法:

    /**
     * 阿拉伯數字轉換成中文,小數點後四捨五入保留兩位.
     * 使用於整數、小數的轉換.
     * @param amount 
     * @return
     */
    public static String numToChinese(double amount) {

        if(amount > 99999999999999.99 || amount < -99999999999999.99)
            throw new IllegalArgumentException("參數值超出容許範圍 (-99999999999999.99 ~ 99999999999999.99)!");

        boolean negative = false;
        if(amount < 0) {
            negative = true;
            amount = amount * (-1);
        }

        long temp = Math.round(amount * 100);
        int numFen = (int)(temp % 10); 
        temp = temp / 10;
        int numJiao = (int)(temp % 10);
        temp = temp / 10;

        int[] parts = new int[20]; 
        int numParts = 0; 
        for(int i=0; ; i++) {
            if(temp ==0)
                break;
            int part = (int)(temp % 10000);
            parts[i] = part;
            numParts ++;
            temp = temp / 10000;
        }

        boolean beforeWanIsZero = true; // 標誌「萬」下面一級是否是 0

        String chineseStr = "";
        for(int i=0; i<numParts; i++) {

            String partChinese = toChinese(parts[i]);
            if(i % 2 == 0) {
                if("".equals(partChinese))
                    beforeWanIsZero = true;
                else
                    beforeWanIsZero = false;
            }

            if(i != 0) {
                if(i % 2 == 0)
                    chineseStr = "億" + chineseStr;
                else {
                    if("".equals(partChinese) && !beforeWanIsZero)   // 若是「萬」對應的 part 爲 0,而「萬」下面一級不爲 0,則不加「萬」,而加「零」
                        chineseStr = "零" + chineseStr;
                    else {
                        if(parts[i-1] < 1000 && parts[i-1] > 0) // 若是"萬"的部分不爲 0, 而"萬"前面的部分小於 1000 大於 0, 則萬後面應該跟「零」
                            chineseStr = "零" + chineseStr;
                        chineseStr = "萬" + chineseStr;
                    }
                }
            }
            chineseStr = partChinese + chineseStr;
        }

        if("".equals(chineseStr))  // 整數部分爲 0, 則表達爲"零"
            chineseStr = numArray[0];
        else if(negative) // 整數部分不爲 0
            chineseStr = "負" + chineseStr;

        chineseStr = chineseStr + "";

        if(numFen == 0 && numJiao == 0) {
            chineseStr = chineseStr + "";
        }else if(numFen == 0) { 
            chineseStr = chineseStr +"點"+ numArray[numJiao] + "";
        }else { // 「分」數不爲 0
            if(numJiao == 0)
                chineseStr = chineseStr + "零" + numArray[numFen] + "";
            else
                chineseStr = chineseStr +"點"+ numArray[numJiao] + numArray[numFen] + "";
        }

        return chineseStr;

    }


    /**
     * 把一個 0~9999 之間的整數轉換爲漢字的字符串,若是是 0 則返回 ""
     * @param amountPart
     * @return
     */
    private static String toChinese(int amountPart) {

        if(amountPart < 0 || amountPart > 10000) {
            throw new IllegalArgumentException("參數必須是大於等於 0,小於 10000 的整數!");
        }

        String[] units = new String[] {"", "十", "百", "千"};

        int temp = amountPart;

        String amountStr = new Integer(amountPart).toString();
        int amountStrLength = amountStr.length();
        boolean lastIsZero = true; //在從低位往高位循環時,記錄上一位數字是否是 0
        String chineseStr = "";

        for(int i=0; i<amountStrLength; i++) {
            if(temp == 0)  // 高位已無數據
                break;
            int digit = temp % 10;
            if(digit == 0) { // 取到的數字爲 0
                if(!lastIsZero)  //前一個數字不是 0,則在當前漢字串前加「零」字;
                    chineseStr = "零" + chineseStr;
                lastIsZero = true;
            }
            else { // 取到的數字不是 0
                chineseStr = numArray[digit] + units[i] + chineseStr;
                lastIsZero = false;
            }
            temp = temp / 10;
        }
        return chineseStr;
    }

3、適用於金額的轉換方法:

/**
	 * 適用於金額轉換.
	 * @param amount 
	 * @return
	 */
	public static String amountToChinese(double amount) {

        if(amount > 99999999999999.99 || amount < -99999999999999.99)
            throw new IllegalArgumentException("參數值超出容許範圍 (-99999999999999.99 ~ 99999999999999.99)!");

        boolean negative = false;
        if(amount < 0) {
            negative = true;
            amount = amount * (-1);
        }

        long temp = Math.round(amount * 100);
        int numFen = (int)(temp % 10); // 分
        temp = temp / 10;
        int numJiao = (int)(temp % 10); //角
        temp = temp / 10;
        //temp 目前是金額的整數部分

        int[] parts = new int[20]; // 其中的元素是把原來金額整數部分分割爲值在 0~9999 之間的數的各個部分
        int numParts = 0; // 記錄把原來金額整數部分分割爲了幾個部分(每部分都在 0~9999 之間)
        for(int i=0; ; i++) {
            if(temp ==0)
                break;
            int part = (int)(temp % 10000);
            parts[i] = part;
            numParts ++;
            temp = temp / 10000;
        }

        boolean beforeWanIsZero = true; // 標誌「萬」下面一級是否是 0

        String chineseStr = "";
        for(int i=0; i<numParts; i++) {

            String partChinese = partTranslate(parts[i]);
            if(i % 2 == 0) {
                if("".equals(partChinese))
                    beforeWanIsZero = true;
                else
                    beforeWanIsZero = false;
            }

            if(i != 0) {
                if(i % 2 == 0)
                    chineseStr = "億" + chineseStr;
                else {
                    if("".equals(partChinese) && !beforeWanIsZero)   // 若是「萬」對應的 part 爲 0,而「萬」下面一級不爲 0,則不加「萬」,而加「零」
                        chineseStr = "零" + chineseStr;
                    else {
                        if(parts[i-1] < 1000 && parts[i-1] > 0) // 若是"萬"的部分不爲 0, 而"萬"前面的部分小於 1000 大於 0, 則萬後面應該跟「零」
                            chineseStr = "零" + chineseStr;
                        chineseStr = "萬" + chineseStr;
                    }
                }
            }
            chineseStr = partChinese + chineseStr;
        }

        if("".equals(chineseStr))  // 整數部分爲 0, 則表達爲"零元"
            chineseStr = chineseDigits[0];
        else if(negative) // 整數部分不爲 0, 而且原金額爲負數
            chineseStr = "負" + chineseStr;

        chineseStr = chineseStr + "元";

        if(numFen == 0 && numJiao == 0) {
            chineseStr = chineseStr + "整";
        }else if(numFen == 0) { // 0 分,角數不爲 0
            chineseStr = chineseStr + chineseDigits[numJiao] + "角";
        }else { // 「分」數不爲 0
            if(numJiao == 0)
                chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分";
            else
                chineseStr = chineseStr + chineseDigits[numJiao] + "角" + chineseDigits[numFen] + "分";
        }

        return chineseStr;

    }
4、測試方法:

public static void main(String[] args) {

        if(args.length == 0) {
            System.out.println("轉換演示:");
            System.out.println("------------金額測算開始-------------");
            System.out.println("25000000000005.999: " + amountToChinese(25000000000005.999));
            System.out.println("45689263.626: " + amountToChinese(45689263.626));
            System.out.println("0.69457: " + amountToChinese(0.69457));
            System.out.println("253.0: " + amountToChinese(253.0));
            System.out.println("0: " + amountToChinese(0));
            System.out.println("2096: "+ amountToChinese(56));
            System.out.println("-----------金額測算結束--------------");
            
            System.out.println("-----------數字測算開始--------------");
            System.out.println("10889.72356: "+numToChinese(10889.72356));
            System.out.println("12653: "+numToChinese(12653));
            System.out.println("215.6387: "+numToChinese(215.6387));
            System.out.println("-----------數字測算結束--------------");
        }
        else {
            System.out.println("轉換結果:");
            System.out.println(args[0] + ": " + amountToChinese(Double.parseDouble(args[0])));
        }
    }

5、測試結果:



版權聲明:本文爲博主原創文章,未經博主容許不得轉載。code

相關文章
相關標籤/搜索