瀏覽代碼

公式-换算值
1、计算平均值 精度问题处理

LHB 2 周之前
父節點
當前提交
c8e780005d
共有 1 個文件被更改,包括 14 次插入14 次删除
  1. 14 14
      blade-service/blade-manager/src/main/java/com/mixsmart/utils/CustomFunction.java

+ 14 - 14
blade-service/blade-manager/src/main/java/com/mixsmart/utils/CustomFunction.java

@@ -3124,7 +3124,7 @@ public class CustomFunction {
      * @param surveyDepth 测区深度
      * @param Pumping 是否泵送 1-是,2-否
      */
-    public static Object concreteStrength(Object data, Object angle, Object pouringSurface, Object type, Object surveyDepth, Object Pumping){
+    public static Object concreteStrength(Object data, Object angle, Object pouringSurface, Object type, Object surveyDepth, Object Pumping) {
         List<Object> datas = obj2ListNe(data);
         List<Object> data1 = obj2ListNe(angle);
         List<Object> data2 = obj2ListNe(pouringSurface);
@@ -3161,23 +3161,23 @@ public class CustomFunction {
                 double h = 0;
 
                 //测区深度 第一种只取第一条数据
-                if(type == null || "1".equals(type.toString())){
+                if (type == null || "1".equals(type.toString())) {
                     String surveyDepthStr;
-                    if(surveyDepth instanceof List){
+                    if (surveyDepth instanceof List) {
                         List<Object> surveyDepthList = obj3ListNe(surveyDepth);
                         surveyDepthStr = surveyDepthList.get(0).toString();
-                    }else{
+                    } else {
                         surveyDepthStr = surveyDepth.toString();
                     }
                     //分割字符串
                     String[] split = surveyDepthStr.split(surveyDepthStr.contains(",") ? "," : "、");
                     //计算平均值
-                    h = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0);
+                    h = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(split.length), 3, RoundingMode.HALF_UP).doubleValue();
                     h = h * 2;
                     //进行0.5修正
-                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(),1);
+                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
                     h = h / 2;
-                }else{
+                } else {
                     List<Object> surveyDepthList = obj3ListNe(surveyDepth);
                     List<Double> doubleArrList = new ArrayList<>();
                     surveyDepthList.forEach(f -> {
@@ -3185,9 +3185,9 @@ public class CustomFunction {
                         //分割字符串
                         String[] split = string.split(string.contains(",") ? "," : "、");
                         //计算平均值
-                        double avg = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0);
+                        double avg = Arrays.stream(split).filter(StringUtils::isNumber).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(split.length), 3, RoundingMode.HALF_UP).doubleValue();
                         avg = avg * 2;
-                        avg = roundHalfEven(new BigDecimal(avg).setScale(1, RoundingMode.HALF_UP).doubleValue(),1);
+                        avg = roundHalfEven(new BigDecimal(avg).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
                         avg = avg / 2;
                         doubleArrList.add(avg);
                     });
@@ -3195,7 +3195,7 @@ public class CustomFunction {
                     h = doubleArrList.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                     h = h * 2;
                     //进行0.5修正
-                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(),1);
+                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
                     h = h / 2;
                 }
 
@@ -3236,7 +3236,7 @@ public class CustomFunction {
     /**
      * 使用银行家舍入法进行0.5修正
      * @param number 要修正的数字
-     * @param scale 保留的小数位数
+     * @param scale  保留的小数位数
      * @return 修正后的结果
      */
     public static double roundHalfEven(double number, int scale) {
@@ -3276,12 +3276,12 @@ public class CustomFunction {
             //如果==0,就按照最开始的整数部分奇偶来计算 奇进偶退
             if (decimalPart2.compareTo(BigDecimal.ZERO) == 0) {
                 //奇数
-                if(!isEven(bigDecimal)){
-                   return v;
+                if (!isEven(bigDecimal)) {
+                    return v;
                 } else {
                     return bigDecimal.doubleValue();
                 }
-            }else{
+            } else {
                 return v;
             }
         }