android工具類,轉換大小寫,保留小數點處理方法



import java.text.DecimalFormat;
import java.util.Scanner;
/**
 * 金額轉換
 * 
 * @author Administrator
 */
public class ConvertMoney {
    
    // 大寫數字
    private final static String[] STR_NUMBER = {"零", "壹", "貳", "叁", "肆", "伍",
            "陸", "柒", "捌", "玖"};
    // 整數單位
    private final static String[] STR_UNIT = {"", "拾", "佰", "仟", "萬", "拾", "佰",
            "仟", "億", "拾", "佰", "仟"};
    // 小數單位
    private final static String[] STR_UNIT2 = {"厘", "分", "角"};

    /**
     * 程序入口
     * @param args
     */
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入金額:");
        // 獲取金額轉換后的字符串
        String convert = ConvertMoney.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對象
        StringBuffer temp = new StringBuffer();
        for (int i = 0; i< num.length(); i++) {// 加入單位
            temp.append(STR_UNIT[i]);
            temp.append(STR_NUMBER[num.charAt(i) - 48]);
            /*
             * num.charAt(i)-48獲取數值或者使用Integer.pa……
             * ASCLL表中0的位置是48,比如得到的字符3,對應ASCLL值 是51,減去48才是這里要的值
             */
        }
        // 反轉字符串
        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++) {
            if (num.length() == 1) {
                temp.append(STR_UNIT2[i + 2]);
                temp.append(STR_NUMBER[num.charAt(i) - 48]);
            } else if (num.length() == 2) {
                temp.append(STR_UNIT2[i + 1]);
                temp.append(STR_NUMBER[num.charAt(i) - 48]);
            } else if (num.length() >= 3) {
                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.###");
        // 格式化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 {
            if (strNum.length() > 12) {
                System.out.println("數字太大,不能完成轉換!");
                return "";
            }
            point = "元整";
        }
        // 轉換結果
        String result = getInteger(strNum) + point + getDecimal(strNum);
        if (result.startsWith("元")) { // 判斷是字符串是否已"元"結尾
            // 截取字符串
            result = result.substring(1, result.length());
        }
        // 返回新的字符串
        return result;
    }
}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容