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

来源:互联网 发布:寻找客户的软件 编辑:程序博客网 时间:2024/05/01 10:53

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;
                }
            }
        }
    }
}

原创粉丝点击