import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
* @ProjectName: LieIdle
* @Package: com.jinzhending.comm
* @Author: huat
* @Date: 2020/4/6 9:04
* @Version: 1.0
*/
public class MoneyConvert {
/**
* 金额为分的格式
*/
public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";
/**
* 将分为单位的转换为元 (除100)
*
* @param amount
* @return
* @throws Exception
*/
public static String fen2YuanStr(String amount) {
if (!amount.matches(CURRENCY_FEN_REGEX)) {
throw new RuntimeException("金额格式错误|"+amount);
}
return formatFen(BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)));
}
/**
* 格式化数字
* @param fen
* @return
*/
private static String formatFen(BigDecimal fen){
DecimalFormat df1 = new DecimalFormat("0.00");
return df1.format(fen);
}
/**
* 将元为单位的参数转换为分 , 只对小数点前2位支持
*
* @param yuan
* @return
* @throws Exception
*/
public static String yuan2FenInt(String yuan){
BigDecimal fenBd = new BigDecimal(yuan).multiply(new BigDecimal(100));
fenBd = fenBd.setScale(0, BigDecimal.ROUND_HALF_UP);
return String.valueOf(fenBd.intValue());
}
public static void main(String[] args) {
System.out.println(fen2YuanStr("1011"));
}