金額轉換爲中文大寫格式

package org.sz.basic;

import java.text.DecimalFormat;

public class ConvertMoney {

    private final static String[] STR_NUMBER = { "零", "壹", "貳", "叄", "肆", "伍",
            "陸", "柒", "捌", "玖" };
    private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "萬", "拾萬",
            "佰萬", "仟萬", "億", "拾億", "佰億", "仟億" };
    private final static String[] STR_UNIT2 = {"釐", "分", "角"};

    public static String convert(double money) {
        DecimalFormat df = new DecimalFormat("#0.000");
        String strMoney = df.format(money);
        
        String point = "";
        
        if (strMoney.indexOf(".") != -1) {
            String integer = strMoney.substring(0, strMoney.indexOf("."));
            checkIntegerLength(integer);
            
            String decimal = strMoney.substring(strMoney.indexOf(".") + 1);
            point = "元";
            return convertInteger(integer) + point + convertDecimal(decimal);
        } else {
            checkIntegerLength(strMoney);
            
            point = "元整";
            return convertInteger(strMoney) + point;
        }
    }
    
    private static void checkIntegerLength(String integer) {
        if (integer.length() > 12) 
            throw new RuntimeException("數字過大不可轉換!");
        
    }
    
    private static String convertInteger(String integer) {
        return appendUnit((replaceNum(integer, STR_NUMBER)), STR_UNIT);
        
    }
    
    private static String convertDecimal(String decimal) {
        return appendUnit((replaceNum(decimal, STR_NUMBER)), STR_UNIT2);
    }
    /**
     * 將小寫數字替換成大寫數字
     */
    private static String replaceNum(String money, String[] number) {
        char[] _money = money.toCharArray();
        String result = "";
        for (int i = 0; i < _money.length; i++) {
            int index = Integer.valueOf(_money[i] + "");
            result += number[index];
        }
        return result;
    }
    /**
     * 爲大寫數字添加單位
     */
    private static String appendUnit(String money, String[] unit) {
        char[] _money = money.toCharArray();
        String result = "";
        String previousNum = "";
        String currentNum = "";
        for (int i = 0; i < money.length(); i++) {
            currentNum = _money[i] + "";
            if (i > 0) {
                previousNum = _money[i - 1] + "";
            }
            //中間連續的0只保留一個
            if (previousNum.equals("零") && currentNum.equals("零")) {
                continue;
            } else if(i != money.length() && currentNum.equals("零")) {
                result += _money[i];
            } else {
                result += (_money[i] + unit[money.length() - i - 1]);
            }
        }
        System.out.println(handlerZero(result));
        return handlerZero(result);
    }
    
    private static String handlerZero(String result) {
        int last = result.lastIndexOf("零");
        if (last == result.length() - 1) {
            result = result.substring(0, last);
        }
        return result;
    }
    
    public static void main(String[] args) {
        convert(30000010000.001);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章