java 人民幣大寫顯示

bei_fang_du_lang發表於2016-11-24
public class NumberToCN {
	/**
     * 漢語中數字大寫
     */
    private static final String[] CN_UPPER_NUMBER = { "零", "壹", "貳", "叄", "肆",
            "伍", "陸", "柒", "捌", "玖" };
    /**
     * 漢語中貨幣單位大寫,這樣的設計類似於佔位符
     */
    private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
            "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "兆", "拾",
            "佰", "仟" };
    /**
     * 特殊字元:整
     */
    private static final String CN_FULL = "整";
    /**
     * 特殊字元:負
     */
    private static final String CN_NEGATIVE = "負";
    /**
     * 金額的精度,預設值為2
     */
    private static final int MONEY_PRECISION = 2;
    /**
     * 特殊字元:零元整
     */
    private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

    /**
     * 把輸入的金額轉換為漢語中人民幣的大寫
     * 
     * @param numberOfMoney
     *            輸入的金額
     * @return 對應的漢語大寫
     */
    public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
        StringBuffer sb = new StringBuffer();
        // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情況
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        //這裡會進行金額的四捨五入
        long number = numberOfMoney.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
        // 得到小數點後兩位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;
        // 判斷最後兩位數,一共有四中情況:00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;
        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次獲取到最後一個數
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            // 讓number每次都去掉最後一個數
            number = number / 10;
            ++numIndex;
        }
        // 如果signum == -1,則說明輸入的數字為負數,就在最前面追加特殊字元:負
        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }
        // 輸入的數字小數點後兩位為"00"的情況,則要在最後追加特殊字元:整
        if (!(scale > 0)) {
            sb.append(CN_FULL);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        double money = 2020004.00;
        BigDecimal numberOfMoney = new BigDecimal(money);
        String s = NumberToCN.number2CNMontrayUnit(numberOfMoney);
        System.out.println("你輸入的金額為:【"+ money +"】   #--# [" +s.toString()+"]");
    }
}

相關文章