1.保留两位小数不进位
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("1234.8888");
String bigDecimal1s = bigDecimal1.setScale(2, RoundingMode.DOWN).toPlainString();
System.out.println(bigDecimal1s);
}
结果:
2.比较大小
if (cspMarketOrderInfoPayment.getConfirmMoney() == null || cspMarketOrderInfoPayment.getConfirmMoney().compareTo(BigDecimal.ZERO) <= 0) {
throw new CustomException("收款金额需要大于0!");
}
if (bean.getOqty().compareTo(BigDecimal.ZERO) > 0) {
throw new CustomException("该商品实际有库存或商品正在出库中,不允许对 商品名称、外部商品编码、最小单位的规格描述、副单位的规格描述、副单位启用状态 进行修改!");
}
3.集合遍历+结果相加
List<CspInventoryInfo> allocationDetailsList = inventoryInfoMapper.selectInventoryAllocationDetailsByLotid(lotid);
BigDecimal oqtyNum = allocationDetailsList.stream()
.filter(i -> i.getOqty() != null)
.map(CspInventoryInfo::getOqty)
.reduce(BigDecimal.ZERO, BigDecimal::add);
4.向上取整保留4位小数(最后一位会进一)
public static void main(String[] args) {
BigDecimal reviewRate = new BigDecimal("0.0000");
reviewRate = new BigDecimal("123")
.divide(new BigDecimal("9999"), 4, BigDecimal.ROUND_UP);
// BigDecimal bigDecimal = reviewRate.setScale(2, BigDecimal.ROUND_DOWN);
System.out.println(reviewRate);
}
complaintRate = new BigDecimal(String.valueOf(workOrderNumber)).divide(new BigDecimal(String.valueOf(orderInfoNumber)), 10, BigDecimal.ROUND_DOWN)
.multiply(new BigDecimal("100"));
dataCockpitWorkOrderResult.setComplaintSummaryRate(complaintRate.setScale(5, BigDecimal.ROUND_UP));
5.除法+乘法
BigDecimal targetReachValue = temperatureCoverageRate.divide(targetValue, 4, BigDecimal.ROUND_UP).multiply(new BigDecimal("100"));