금액 형식 변환
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);
// }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.