字符串转百分数,并求平均数 BigDecimal 、DecimalFormat

 /**
     * 求参评率 和 解决率 的平均数 ,都是百分号的字符串
     * @return
     */
    public List<RptRobotAnswerStatsNew> dealRptRobotAnswerStatsNew(String startDate, String endDate,
                                                               String area, String formats, String companyId){
        List<RptRobotAnswerStatsNew> list = rptRobotAnswerStatsService.getRptRobotAnswerList(companyId, area, formats, startDate, endDate);
        RptRobotAnswerStatsNew total = new RptRobotAnswerStatsNew();
        total.setRobotName("总计"); //汇总数据
       
        BigDecimal canPanLv = new BigDecimal(0.00);
        BigDecimal solvedLv = new BigDecimal(0.00);
        DecimalFormat df = new DecimalFormat("0.00%");
        int i=0;
        for(RptRobotAnswerStatsNew answerStats : list) {
            i++;
            try {
                RptRobotSatisfactionStatsNew rrssn = robotSatisfactionService.getRobotSatisfactionNew(startDate, endDate, null, answerStats.getRobotId(),companyId);
              
                log.info("rrssnStr-->"+ JSON.toJSONString(rrssn));      
                /**参评率**/
                answerStats.setCanpinglv(rrssn.getCanpinglv());        
                /**已解决率**/
                answerStats.setSolvedLv(rrssn.getSolvedLv());      
                String canPingLvStr=answerStats.getCanpinglv().replace("%","");
                canPingLvStr=canPingLvStr.replace("%","");
                String solvedLvStr=answerStats.getCanpinglv().replace("%","");
                solvedLvStr = solvedLvStr.replace("%","");
                Float cplf = Float.valueOf(canPingLvStr) / 100;
                Float slf = Float.valueOf(solvedLvStr) / 100;


                BigDecimal cpl = new BigDecimal(cplf);
                BigDecimal sl = new BigDecimal(slf);
                canPanLv = canPanLv.add(cpl);
                solvedLv = solvedLv.add(sl);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("dealRptRobotAnswerStatsNew Error",e);
            }
        }
       //求完平均数最后在转换成百分数
        total.setCanpinglv(df.format(canPanLv.divide(new BigDecimal(i),4, BigDecimal.ROUND_HALF_UP)));
        total.setSolvedLv(df.format(solvedLv.divide(new BigDecimal(i),4, BigDecimal.ROUND_HALF_UP)));
        list.add(total);
        return  list;
    }

 

发布了169 篇原创文章 · 获赞 213 · 访问量 19万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览