int 做除法運算資料丟失【BigDecimal 重現數字】
部分程式碼如:
//組裝資料
all.forEach(a->{
Optional<WetListVO> re = reWetL.stream().
filter(b->b.getDate().equals( a.getDate())).findFirst();
if(re.isPresent()){
a.setResidentWetCount(re.get().getResidentWetCount());
a.setResidentWetNum(re.get().getResidentWetNum());
}
Optional<WetListVO> bu = buWetL.stream().
filter(b->b.getDate().equals(a.getDate())).findFirst();
if(bu.isPresent()){
a.setResidentBucketCount(bu.get().getResidentBucketCount());
a.setResidentBucketNum(bu.get().getResidentBucketNum());
}
// 計算廚餘重量
List<WetListVO> wedateL = wetL.stream().filter(b->b.getDate().equals(a.getDate()))
.collect(Collectors.toList());
Map<Integer,List<WetListVO>> stationGroup = wedateL.stream()
.collect(Collectors.groupingBy(WetListVO::getStationId));
double kitchenWeight = 0;
for(Entry<Integer,List<WetListVO>> entry :stationGroup.entrySet()){
List<WetListVO> stationList = entry.getValue();
int totalHouseholds = stationList.stream().
mapToInt(WetListVO::getVillageHouseHolds).sum();
int villageHouseHolds = stationList.stream().filter(s->s.getuVillageId() >0)
.collect(Collectors.toList()).stream()
.mapToInt(WetListVO::getVillageHouseHolds).sum();
//BigDecimal
kitchenWeight = kitchenWeight + new BigDecimal(villageHouseHolds).doubleValue()/
totalHouseholds*stationList.get(0).getKitchenWeight();
}
a.setKitchenWeight(new BigDecimal(kitchenWeight).
setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
});
在用villageHouseHolds/totalHouseholds 時候如果villageHouseholds 值太小 這個算下來得就是0,導致沒有結果,用BigDecimal 做如上處理
就會解