Pārlūkot izejas kodu

double devide never throw exception

lixing 3 gadi atpakaļ
vecāks
revīzija
e0aa17fd86

+ 8 - 3
src/main/java/com/persagy/apm/energy/report/common/utils/DataUtils.java

@@ -1,13 +1,16 @@
 package com.persagy.apm.energy.report.common.utils;
 
+import lombok.extern.slf4j.Slf4j;
 import org.nfunk.jep.JEP;
 import org.springframework.util.CollectionUtils;
 
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.List;
 import java.util.stream.Collectors;
 
+@Slf4j
 public class DataUtils {
     /**
      * 对象转Double类型
@@ -123,15 +126,17 @@ public class DataUtils {
         // 结果保留的位数
         int scale = 4;
         if (d1 == null || d2 == null) {
-            throw new IllegalArgumentException("两个double做除法,其中一个为空或两个都为空");
+            log.error("两个double做除法,其中一个为空或两个都为空, d1: {}, d2: {}", d1, d2);
+            return null;
         }
         if (d2.equals(0d)) {
-            throw new IllegalArgumentException("double做除法,被除数不能为0");
+            log.error("double做除法,被除数不能为0");
+            return null;
         }
         BigDecimal bigDecimal1 = new BigDecimal(d1);
         BigDecimal bigDecimal2 = new BigDecimal(d2);
         return bigDecimal1.divide(
-                bigDecimal2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
+                bigDecimal2, scale, RoundingMode.HALF_UP).doubleValue();
     }
 
     /**