1. 程式人生 > >Java 小程式 之將輸入的數字轉換為金額輸出

Java 小程式 之將輸入的數字轉換為金額輸出

import java.text.DecimalFormat;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class ConvertMoney {
    // 大寫數字
    private final static String[] STR_NUMBER = { "零", "壹", "貳", "叄", "肆", "伍",
            "陸", "柒", "捌", "玖" };
    private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "萬", "拾",
            "佰", "仟", "億", "拾", "佰", "仟" };// 整數單位
    private final static String[] STR_UNIT2 = { "分","角" };// 小數單位
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);// 建立掃描器
        System.out.println("請輸入一個金額");
        // 獲取金額轉換後的字串
        String convert = convert(scan.nextDouble());
        System.out.println(convert);// 輸出轉換結果
    }
    
    /**
     * 獲取可數部分
     * 
     * @param num
     *            金額
     * @return 金額整數部分
     */
    public static String getInteger(String num) {
        if (num.indexOf(".") != -1) { // 判斷是否包含小數點
            num = num.substring(0, num.indexOf("."));
        }
        num = new StringBuffer(num).reverse().toString(); // 反轉字串
        StringBuffer temp = new StringBuffer(); // 建立一個StringBuffer物件
        for (int i = 0; i < num.length(); i++) {// 加入單位
            temp.append(STR_UNIT[i]);
            temp.append(STR_NUMBER[num.charAt(i) - 48]); //數字0-9的ASCII碼為48-57
        }
        num = temp.reverse().toString();// 反轉字串
        num = numReplace(num, "零拾", "零"); // 替換字串的字元
        num = numReplace(num, "零佰", "零"); // 替換字串的字元
        num = numReplace(num, "零仟", "零"); // 替換字串的字元
        num = numReplace(num, "零萬", "萬"); // 替換字串的字元
        num = numReplace(num, "零億", "億"); // 替換字串的字元
        num = numReplace(num, "零零", "零"); // 替換字串的字元
        num = numReplace(num, "億萬", "億"); // 替換字串的字元
        // 如果字串以零結尾將其除去
        if (num.lastIndexOf("零") == num.length() - 1) {
            num = num.substring(0, num.length() - 1);
        }
        return num;
    }
    
    /**
     * 獲取小數部分
     * 
     * @param num
     *            金額
     * @return 金額的小數部分
     */
    public static String getDecimal(String num) {
        // 判斷是否包含小數點
        if (num.indexOf(".") == -1) {
            return "";
        }
        num = num.substring(num.indexOf(".") + 1);
        // 反轉字串
        num = new StringBuffer(num).reverse().toString();
        // 建立一個StringBuffer物件
        StringBuffer temp = new StringBuffer();
        // 加入單位
        for (int i = 0; i < num.length(); i++) {
            temp.append(STR_UNIT2[i]);
            temp.append(STR_NUMBER[num.charAt(i) - 48]);
        }
        num = temp.reverse().toString(); // 替換字串的字元
        num = numReplace(num, "零角", "零"); // 替換字串的字元
        num = numReplace(num, "零分", "零"); // 替換字串的字元
        num = numReplace(num, "零釐", "零"); // 替換字串的字元
        num = numReplace(num, "零零", "零"); // 替換字串的字元
        // 如果字串以零結尾將其除去
        if (num.lastIndexOf("零") == num.length() - 1) {
            num = num.substring(0, num.length() - 1);
        }
        return num;
    }
    
    /**
     * 替換字串中內容
     * 
     * @param num
     *            字串
     * @param oldStr
     *            被替換內容
     * @param newStr
     *            新內容
     * @return 替換後的字串
     */
    public static String numReplace(String num, String oldStr, String newStr) {
        while (true) {
            // 判斷字串中是否包含指定字元
            if (num.indexOf(oldStr) == -1) {
                break;
            }
            // 替換字串
            num = num.replaceAll(oldStr, newStr);
        }
        // 返回替換後的字串
        return num;
    }
    
    /**
     * 金額轉換
     * 
     * @param d
     *            金額
     * @return 轉換成大寫的全額
     */
    public static String convert(double d) {
        // 例項化DecimalFormat物件
        DecimalFormat df = new DecimalFormat("#0.##"); //#沒有則為空,0沒有則補0
        // 格式化double數字
        String strNum = df.format(d);
        // 判斷是否包含小數點
        if (strNum.indexOf(".") != -1) {
            String num = strNum.substring(0, strNum.indexOf("."));
            // 整數部分大於12不能轉換
            if (num.length() > 12) {
                System.out.println("數字太大,不能完成轉換!");
                return "";
            }
        }
        String point = "";// 小數點
        if (strNum.indexOf(".") != -1) {
            point = "元";
        } else {
            point = "元整";
        }
        // 轉換結果
        String result = getInteger(strNum) + point + getDecimal(strNum);
        if (result.startsWith("元")) { // 判斷是字串是否以"元"開頭,即沒有整數部分,只有小數部分
            result = result.substring(1, result.length()); // 擷取字串
        }
        return result; // 返回新的字串
    }
}