數字の書式を変換するクラス

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * 數字の書式を変換するクラス<BR>
 *
 * <PRE>
 * 數字の書式を変換する
 * </PRE>
 *
 * @author TCI xxx 2011/12/23
 *
 */
public final class NumberUtil {

    /** 定數DEFAULT_MONEY_FORMAT_STR1常用金額の書式 */
    private static final String DEFAULT_MONEY_FORMAT_STR1 = "###,###,###.00";

    /**
     * 常用數字の書式を変換するメソッド<BR>
     *
     * <PRE>
     * 常用數字の書式を変換するする
     * </PRE>
     *
     * @param _number
     *            処理の數字のパラメータ
     * @return 數字の書式を変換する結果
     *
     */
    public static String numberFormat(int _number) {
        NumberFormat numf = NumberFormat.getInstance();
        return numf.format(_number);
    }

    /**
     * 常用金額の書式を変換するメソッド<BR>
     *
     * <PRE>
     * 常用金額の書式を変換するする
     * </PRE>
     *
     * @param _money
     *            処理の金額のパラメータ
     * @return 金額の書式を変換する結果
     *
     */
    public static String moneyFormat(double _money) {
        DecimalFormat decimalFormat = new DecimalFormat(
                DEFAULT_MONEY_FORMAT_STR1);
        return decimalFormat.format(_money);
    }

    /**
     * 常用金額の書式を変換するメソッド<BR>
     *
     * <PRE>
     * 常用金額の書式を変換するする
     * </PRE>
     *
     * @param _money
     *            処理の金額のパラメータ
     * @return 金額の書式を変換する結果
     *
     */
    public static String moneyFormatZz(BigDecimal _money) {
        String money = _money.toString();
        int length = money.length();
        int index = money.indexOf(".");
        DecimalFormat decimalFormat = new DecimalFormat("###,###,###");

        if (index == -1) {
            String strMoney = decimalFormat.format(Double.parseDouble(money));
            return strMoney;
        } else {
            String str0 = money.substring(index + 1, length);
            if (str0.length() == 2) {
                String str1 = str0.substring(0, 1);
                String str2 = str0.substring(1, 2);
                if ("0".equals(str2)) {
                    if ("0".equals(str1)) {
                        // 9999.00
                        String strZhengNum = money.substring(0, index);
                        String strZhengMoney = decimalFormat.format(Double
                                .parseDouble(strZhengNum));
                        return strZhengMoney;
                    } else {
                        // 9999.90
                        String strZhengNum = money.substring(0, index);
                        String strZhengMoney = decimalFormat.format(Double
                                .parseDouble(strZhengNum));
                        String strMoney = strZhengMoney + "." + str1;
                        return strMoney;
                    }
                } else {
                    // 9999.99
                    String strXiaoNum = money.substring(index, length);
                    String strZhengNum = money.substring(0, index);
                    String strZhengMoney = decimalFormat.format(Double
                            .parseDouble(strZhengNum));
                    String strMoney = strZhengMoney + strXiaoNum;
                    return strMoney;
                }
            } else {
                // 9999.Z
                String str1 = str0.substring(0, 1);
                if ("0".equals(str1)) {
                    // 9999.0
                    String strZhengNum = money.substring(0, index);
                    String strZhengMoney = decimalFormat.format(Double
                            .parseDouble(strZhengNum));
                    return strZhengMoney;
                } else {
                    // 9999.9
                    String strXiaoNum = money.substring(index, length);
                    String strZhengNum = money.substring(0, index);
                    String strZhengMoney = decimalFormat.format(Double
                            .parseDouble(strZhengNum));
                    String strMoney = strZhengMoney + strXiaoNum;
                    return strMoney;
                }
            }
        }
    }
}

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