BigDecimal
public static Double roundConvert(BigDecimal val, Integer computeRound){
BigDecimal result = BigDecimal.ZERO;
switch (computeRound){
case ComputeRoundConstant.COMPUTE_ROUND1:
result = val.setScale(2, BigDecimal.ROUND_HALF_UP);
break;
case ComputeRoundConstant.COMPUTE_ROUND2:
result = val.setScale(1, BigDecimal.ROUND_HALF_UP);
break;
case ComputeRoundConstant.COMPUTE_ROUND3:
result = val.setScale(0, BigDecimal.ROUND_DOWN );
break;
case ComputeRoundConstant.COMPUTE_ROUND4:
result = val.setScale( 0, BigDecimal.ROUND_UP );
break;
case ComputeRoundConstant.COMPUTE_ROUND5:
BigDecimal intValue = val.setScale(0,BigDecimal.ROUND_DOWN);
BigDecimal subValue = val.subtract(intValue);
BigDecimal scaleHalf = new BigDecimal("0.5");
if(scaleHalf.compareTo(subValue) == 0){
result = val.setScale(1, BigDecimal.ROUND_DOWN);
}else if(subValue.compareTo(scaleHalf) == 1){
result = intValue.add(BigDecimal.ONE).setScale(1, BigDecimal.ROUND_DOWN);
}else if(subValue.compareTo(scaleHalf) == -1){
if(subValue.compareTo(BigDecimal.ZERO) == 0){
result = intValue.add(subValue).setScale(1, RoundingMode.FLOOR);
}else{
result = intValue.add(scaleHalf).setScale(1, BigDecimal.ROUND_DOWN);
}
}
break;
case ComputeRoundConstant.COMPUTE_ROUND6:
result = val.setScale(0, BigDecimal.ROUND_HALF_UP);
break;
default:
log.error("前置费用转换没有匹配到到计算进制");
}
return Double.parseDouble(result.toString());
}
private static Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+");
public static Integer getNumberOfDecimalPlace(BigDecimal bigDecimal) {
final String str = bigDecimal.toPlainString();
final int index = str.indexOf('.');
if (index < 0) {
return 0;
}
return str.length() - 1 - index;
}
public static boolean isNumeric(String str){
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static Integer getNumberOfDecimalPlace(BigDecimal bigDecimal) {
final String str = bigDecimal.toPlainString();
final int index = str.indexOf('.');
if (index < 0) {
return 0;
}
return str.length() - 1 - index;
}
private static final String matchNumberStr = "-?[0-9]+.?[0-9]*";
public static boolean isNumeric(String target){
if(StrUtil.isBlank(target)){
return false;
}
return target.matches(matchNumberStr);
}