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 做如上处理

就会解决掉这个问题。

转载于:https://my.oschina.net/kuchawyz/blog/3059042

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值