import java.math.BigDecimal; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @ Amount format conversion. */ public class AmountUtil { private static final BigDecimal MAG = BigDecimal.valueOf(1000); private static final BigDecimal MAG4 = BigDecimal.valueOf(10000); /** * Retain decimal places */ private static final int SCALE_VALUE = 3; public static final int DEFAULT_MONERY_SCALE_VALUE = 2; /** *Round to two decimal places * * @param amount * @return amount rounded result */ public static double formatAmount(double amount) { BigDecimal b = new BigDecimal(amount); return b.setScale(SCALE_VALUE, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * Verify that the input string is in the amount format * * @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(); } /** * format * @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; } /** * Divide by 1000 to get the real money value * @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); } /** * Divide by 1000 to get the real money value (retain 3 decimal places) * @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); } /** * Divide by 1000 to get the real money value * @param money * @return */ public static BigDecimal divide1000(BigDecimal money) { if (money == null) { return null; } return money.divide(MAG).setScale(2, BigDecimal.ROUND_HALF_UP); } /** * Divide by 10000 to get the real money value * @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); } /** * Divide by 10000 to get the real money value, keep 4 as a decimal * @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); } /** * Divide by 10000 to get the real money value * @param money * @return */ public static BigDecimal divide10000(BigDecimal money) { if (money == null) { return null; } return money.divide(MAG4).setScale(2, BigDecimal.ROUND_HALF_UP); } /** * Multiply by 1000 to get the money value for calculation * @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(); } /** * Multiply by 10000 to get the money value for calculation * @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(); } /** * Multiply by 1000 to get the money value for calculation * @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(); } /** * Multiply by 1000 to get the money value for calculation * @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); // } }