금액 형식 변환

5237 단어 자바

 
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @       .
 */
public class AmountUtil {

    private static final BigDecimal MAG = BigDecimal.valueOf(1000);
    private static final BigDecimal MAG4 = BigDecimal.valueOf(10000);
    /**
     *       
     */
    private static final int SCALE_VALUE = 3;
    public static final int DEFAULT_MONERY_SCALE_VALUE = 2;

    /**
     *           
     * 
     * @param amount
     * @return amount      
     */
    public static double formatAmount(double amount) {
        BigDecimal b = new BigDecimal(amount);
        return b.setScale(SCALE_VALUE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     *                
     * 
     * @param amount
     * @return
     */
    public static boolean checkAmount(String amountString) {
        if (null == amountString || amountString.trim().length() == 0) {
            return false;
        }
        amountString = amountString.trim();
        String str = "^(0(\\.\\d{0,2})?|([1-9]+[0]*)+(\\.\\d{0,2})?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(amountString);
        return m.find();
    }

    /**
     *    
     * @param amount
     * @return
     */
    public static String formatAmountStr(String amountString) {
        int pos = amountString.indexOf(".");
        if (pos != -1) {
            int tempInt = amountString.substring(pos + 1, amountString.length()).length();
            if (tempInt == 1) {
                amountString += "0";
            }
            if (tempInt > 2) {
                amountString = amountString.substring(0, pos + 3);
            }
        } else {
            amountString += ".00";
        }
        return amountString;
    }

    /**
     *  1000      
     * @param money
     * @return
     */
    public static BigDecimal divide1000(Long money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.divide(MAG).setScale(DEFAULT_MONERY_SCALE_VALUE, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *  1000      (  3   )
     * @param money
     * @return
     */
    public static BigDecimal divide1000Scale3(Long money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.divide(MAG).setScale(3, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *  1000      
     * @param money
     * @return
     */
    public static BigDecimal divide1000(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.divide(MAG).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *  10000      
     * @param money
     * @return
     */
    public static BigDecimal divide10000(Long money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.divide(MAG4).setScale(DEFAULT_MONERY_SCALE_VALUE, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *  10000      ,  4   
     * @param money
     * @return
     */
    public static BigDecimal divide10000For4Scale(Long money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.divide(MAG4).setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *  10000      
     * @param money
     * @return
     */
    public static BigDecimal divide10000(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.divide(MAG4).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *   1000       
     * @param money
     * @return
     */
    public static Long multiply1000(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.multiply(MAG).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     *   10000       
     * @param money
     * @return
     */
    public static Long multiply10000(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.multiply(MAG4).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     *   1000       
     * @param money
     * @return
     */
    public static Long multiply1000(Double money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.multiply(MAG).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     *   1000       
     * @param money
     * @return
     */
    public static Long multiply1000(Long money) {
        if (money == null) {
            return null;
        }
        BigDecimal result = BigDecimal.valueOf(money);
        return result.multiply(MAG).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

//    public static void main(String[] args) {
//        long id = 1234567890L;
//        BigDecimal bigDecimal = AmountUtil.divide1000Scale3(id);
//        System.out.println(bigDecimal);
//    }
}

 
 
 
 

좋은 웹페이지 즐겨찾기