//1.求样本原始平均值
String groupHumanValuesAverage = getGroupHumanValuesAverage(humanValues);
BigDecimal averageVal = new BigDecimal(groupHumanValuesAverage);
System.out.println("原始平均值:"+averageVal.toPlainString());
//2.找到非ND替换而来的数据样本
List<HumanValue> validSamples = humanValues.stream().filter(humanValue -> !humanValue.isNdReplace()).collect(Collectors.toList());
//3.判断是整数还是小数
BigDecimal firstSampleNum = new BigDecimal(CollUtil.getFirst(validSamples).getActValue());
String firstSample = firstSampleNum.toPlainString();
boolean isInteger = !firstSample.contains(".");
//4.若为整数,则对均值四舍五入保留0位小数
if (isInteger) {
BigDecimal divide = averageVal.setScale(0, RoundingMode.HALF_UP);
int deg = firstSample.length() - divide.toString().length();
int scale = deg;
divide = averageVal.setScale(scale,RoundingMode.HALF_UP);
System.out.println("最终的平均值:"+divide.toPlainString());
return divide.toPlainString();
}
//5.若为小数
int sampleIntegerPlaces = StrUtil.split(firstSample, StrUtil.DOT).get(0).length();
int sampleDecimalPlaces = StrUtil.split(firstSample, StrUtil.DOT).get(1).length();
int finalDecimalPlaces = sampleDecimalPlaces;
//6.获取平均值的整数位数
int averageIntegerPlaces = StrUtil.length(String.valueOf(averageVal.intValue()));
//7.如果平均值的整数位数比样本的整数位数大N位,则意味着小数位要比样本值少出N位
int integerPlacesCompare = averageIntegerPlaces - sampleIntegerPlaces;
finalDecimalPlaces = finalDecimalPlaces - integerPlacesCompare;
//8.最终小数位数保留
BigDecimal finalAverageVal = averageVal.setScale(finalDecimalPlaces, RoundingMode.HALF_UP);
System.out.println("最终的平均值:"+finalAverageVal.toPlainString());
return finalAverageVal.toPlainString();
[点击并拖拽以移动]
数据位数相同处理
最新推荐文章于 2024-08-16 23:08:28 发布