计算年度增长率:
增长率公式: 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.setCountNumber100);
personInfo.setCreateTime"2016");
PersonInfo personInfo2=new PersonInfo);
personInfo2.setCountNumber450);
personInfo2.setCreateTime"2017");
PersonInfo personInfo4=new PersonInfo);
personInfo4.setCountNumber22);
personInfo4.setCreateTime"2018");
PersonInfo personInfo5=new PersonInfo);
personInfo5.setCountNumber100);
personInfo5.setCreateTime"2019");
PersonInfo personInfo6=new PersonInfo);
personInfo6.setCountNumber200);
personInfo6.setCreateTime"2020");
list.addpersonInfo);
list.addpersonInfo2);
list.addpersonInfo4);
list.addpersonInfo5);
list.addpersonInfo6);
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.get0);
personInfo0.setGrowthRate"0%");
person0.addpersonInfo0);
//第一项不做比较
if CollectionUtils.isNotEmptyperson)){
for int i = 1; i < person.size); i++) {
PersonInfo personInfo=new PersonInfo);
Integer countNumber = person.geti).getCountNumber);
Integer countNumber1 = person.geti - 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.setGrowthRategrowthRate);
}else{
//如果相等,增长率为 0
personInfo.setGrowthRate"0%");
}
personInfo.setCountNumberperson.geti).getCountNumber));
personInfo.setCreateTimeperson.geti).getCreateTime));
person2.addpersonInfo);
}
}
//list合并
result.addAllperson0);
result.addAllperson2);
return result;
}
public static void mainString[] args) {
List<PersonInfo> analysisData = getAnalysisData);
if CollectionUtils.isNotEmptyanalysisData)){
analysisData.stream).forEachitem->{
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 percentBigDecimalBigDecimal preNum, BigDecimal sufNum){ double result = countDecimalpreNum,sufNum); ifresult>0){ return "+"+result+"%"; } ifresult<0){ return result+"%"; } ifresult==0){ return "+"+0+"%"; } return null; } public double countDecimalBigDecimal preNum,BigDecimal sufNum){ boolean preBoolean = verifyNumpreNum); boolean sufBoolean = verifyNumsufNum); //同时为true计算 ifpreBoolean && sufBoolean){ boolean b = verifyEqualpreNum, sufNum); if b == false){ return realCountDecimalpreNum,sufNum); } if b){ return 0; } } ifpreBoolean == false && sufBoolean ==false){ return 0; } ifsufBoolean ==false){ return 100; } return 0; } //验证数字是否为零和null public boolean verifyNumBigDecimal num){ ifnull !=num && num.compareToBigDecimal.ZERO)!=0 ){ return true; } return false; } //验证两个数字是否相等 public boolean verifyEqualBigDecimal preNum,BigDecimal sufNum){ int n = preNum.compareTosufNum); //比较 -1 小于 0 等于 1 大于 ifn==0){ return true; } return false; } //真正计算 public double realCountDecimalBigDecimal preNum,BigDecimal sufNum){ //前面的数字-后面的数字)/后面的数字*100 BigDecimal bigDecimal = preNum.subtractsufNum)).dividesufNum).multiplynew BigDecimal"100")).setScale2, BigDecimal.ROUND_UP); if bigDecimal.compareToBigDecimal.ZERO) !=0){ return bigDecimal.doubleValue); } return 0; } public static void mainString[] args) { GrowthRate p = new GrowthRate); BigDecimal a = new BigDecimal"3"); BigDecimal b = new BigDecimal"2"); String percent = p.percentBigDecimala, b); System.out.printlnpercent); } }

