zoukankan      html  css  js  c++  java
  • java 计算年增长率 案例

    计算年度增长率:
    增长率公式: (a-b)b*100+'%'   =》(前面的数字-后面的数字)/后面的数字*100

    package cn.demo;
    import org.apache.commons.collections.CollectionUtils;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * @author Administrator
    * @date 2020/7/31
    */
    public class GrowthRateDemo {
    /**
    * 初始化数据
    * @return
    */
    public static List<PersonInfo> getPerson(){
    List<PersonInfo> list=new ArrayList<>();

    PersonInfo personInfo=new PersonInfo();
    personInfo.setCountNumber(100);
    personInfo.setCreateTime("2016");

    PersonInfo personInfo2=new PersonInfo();
    personInfo2.setCountNumber(450);
    personInfo2.setCreateTime("2017");

    PersonInfo personInfo4=new PersonInfo();
    personInfo4.setCountNumber(22);
    personInfo4.setCreateTime("2018");

    PersonInfo personInfo5=new PersonInfo();
    personInfo5.setCountNumber(100);
    personInfo5.setCreateTime("2019");

    PersonInfo personInfo6=new PersonInfo();
    personInfo6.setCountNumber(200);
    personInfo6.setCreateTime("2020");

    list.add(personInfo);
    list.add(personInfo2);
    list.add(personInfo4);
    list.add(personInfo5);
    list.add(personInfo6);
    return list;
    }

    /**
    * 计算年度增长率
    * <p>
    * 以年度分组
    * <p>
    * 增长率: (a-b)b*100+'%' =》(前面的数字-后面的数字)/后面的数字*100
    */
    public static List<PersonInfo> getAnalysisData(){
    List<PersonInfo> result =new ArrayList<>();
    List<PersonInfo> person2 =new ArrayList<>();
    List<PersonInfo> person0=new ArrayList<>();

    List<PersonInfo> person = getPerson();
    //第1项
    PersonInfo personInfo0 = person.get(0);
    personInfo0.setGrowthRate("0%");
    person0.add(personInfo0);

    //第一项不做比较
    if (CollectionUtils.isNotEmpty(person)){
    for (int i = 1; i < person.size(); i++) {
    PersonInfo personInfo=new PersonInfo();
    Integer countNumber = person.get(i).getCountNumber();
    Integer countNumber1 = person.get(i - 1).getCountNumber();
    if (countNumber<countNumber1){
    //如果下个数大于上个数,则增长率 为 正
    String growthRate=(countNumber1-countNumber)/countNumber*100+"%";
    personInfo.setGrowthRate("-"+growthRate);
                    }else if (countNumber>countNumber1){
    //如果下个数小于上个数,则增长率 为 负
    String growthRate= (countNumber-countNumber1)/countNumber1*100+"%";
    personInfo.setGrowthRate(growthRate);
    }else{
    //如果相等,增长率为 0
    personInfo.setGrowthRate("0%");
    }
    personInfo.setCountNumber(person.get(i).getCountNumber());
    personInfo.setCreateTime(person.get(i).getCreateTime());
    person2.add(personInfo);
    }
    }
    //list合并
    result.addAll(person0);
    result.addAll(person2);
    return result;
    }


    public static void main(String[] args) {
    List<PersonInfo> analysisData = getAnalysisData();
    if (CollectionUtils.isNotEmpty(analysisData)){
    analysisData.stream().forEach(item->{
    System.out.println("数量:"+item.getCountNumber());
    System.out.println("增长率:"+item.getGrowthRate());
    System.out.println("时间:"+item.getCreateTime());
    System.out.println("*****************************************************");
    });
    }
    }
    }

     

    工具类

    package cn.demo;
    
    import java.math.BigDecimal;
    
    /**
     * 求增长率
     */
    public class GrowthRate {
        public String percentBigDecimal(BigDecimal preNum, BigDecimal sufNum){
            double result = countDecimal(preNum,sufNum);
            if(result>0){
                return "+"+result+"%";
            }
            if(result<0){
                return result+"%";
            }
            if(result==0){
                return "+"+0+"%";
            }
            return null;
        }
        public  double countDecimal(BigDecimal preNum,BigDecimal sufNum){
            boolean preBoolean = verifyNum(preNum);
            boolean sufBoolean = verifyNum(sufNum);
            //同时为true计算
            if(preBoolean && sufBoolean){
                boolean b = verifyEqual(preNum, sufNum);
                if (b == false){
                    return realCountDecimal(preNum,sufNum);
                }
                if (b){
                    return 0;
                }
            }
            if(preBoolean == false && sufBoolean ==false){
                return 0;
            }
            if(sufBoolean ==false){
                return 100;
            }
            return  0;
        }
        //验证数字是否为零和null
        public boolean verifyNum(BigDecimal num){
            if(null !=num && num.compareTo(BigDecimal.ZERO)!=0 ){
                return true;
            }
            return false;
        }
    
        //验证两个数字是否相等
        public boolean verifyEqual(BigDecimal preNum,BigDecimal sufNum){
            int n = preNum.compareTo(sufNum);
            //比较 -1 小于   0 等于    1 大于
            if(n==0){
                return true;
            }
            return false;
        }
        //真正计算
        public double realCountDecimal(BigDecimal preNum,BigDecimal sufNum){
            //(前面的数字-后面的数字)/后面的数字*100
            BigDecimal bigDecimal = (preNum.subtract(sufNum)).divide(sufNum).multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_UP);
            if (bigDecimal.compareTo(BigDecimal.ZERO) !=0){
                return  bigDecimal.doubleValue();
            }
            return 0;
        }
    
        public static void main(String[] args) {
            GrowthRate p = new GrowthRate();
            BigDecimal a = new BigDecimal("3");
            BigDecimal b = new BigDecimal("2");
            String percent = p.percentBigDecimal(a, b);
            System.out.println(percent);
        }
    
    }
  • 相关阅读:
    poj 3068 Bridge Across Islands
    XidianOJ 1086 Flappy v8
    XidianOJ 1036 分配宝藏
    XidianOJ 1090 爬树的V8
    XidianOJ 1088 AK后的V8
    XidianOJ 1062 Black King Bar
    XidianOJ 1091 看Dota视频的V8
    XidianOJ 1098 突击数论前的xry111
    XidianOJ 1019 自然数的秘密
    XidianOJ 1109 Too Naive
  • 原文地址:https://www.cnblogs.com/weigy/p/13413403.html
Copyright © 2011-2022 走看看