deason 6 months ago
parent
commit
1dd9d1dc63

+ 3 - 3
src/main/java/cn/com/qmth/examcloud/tool/service/batch_import_exam_student/BatchImportExamStudentTask.java

@@ -212,7 +212,7 @@ public class BatchImportExamStudentTask implements TaskService {
                     int runCount = successCount.get() + failCount.get();
                     log.info("总数:{} 成功数:{} 失败数:{} 已执行:{}条 已耗时:{}秒 平均每秒{}条 进度:{}% {}"
                             , total, successCount.get(), failCount.get(), runCount, cost, runCount / cost,
-                            runCount * 100f / total, predictTime(total, runCount, cost));
+                            MathUtils.percentage(runCount, total), predictTime(total, runCount, cost));
                 }
 
                 log.info("----------> 分批集合{} size:{}", n + 1, cutTotal);
@@ -252,7 +252,7 @@ public class BatchImportExamStudentTask implements TaskService {
         }
         float nPerSecond = (float) finishCount / curCost;
         float second = (totalCount - finishCount) / nPerSecond;
-        return "剩:" + second / 60f + "分钟";
+        return "剩:" + MathUtils.round(second / 60f, 2) + "分钟";
     }
 
     private void normalRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount,
@@ -279,7 +279,7 @@ public class BatchImportExamStudentTask implements TaskService {
             if (runCount % 20 == 0) {
                 log.info("总数:{} 成功数:{} 失败数:{} 已执行:{}条 已耗时:{}秒 平均每秒{}条 进度:{}%"
                         , batchList.size(), successCount.get(), failCount.get(), runCount, cost
-                        , runCount / cost, runCount * 100f / batchList.size());
+                        , runCount / cost, MathUtils.percentage(runCount, batchList.size()));
             }
         }
     }

+ 35 - 0
src/main/java/cn/com/qmth/examcloud/tool/utils/MathUtils.java

@@ -0,0 +1,35 @@
+package cn.com.qmth.examcloud.tool.utils;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+public class MathUtils {
+
+    /**
+     * 计算百分比 (默认保留2位小数,超出位数则截断不四舍五入)
+     */
+    public static double percentage(double value1, double value2) {
+        if (value2 == 0d) {
+            return 0d;
+        }
+        double x = value1 * 100d / value2;
+        BigDecimal _value = new BigDecimal(Double.toString(x));
+        BigDecimal one = new BigDecimal("1");
+        return _value.divide(one, 2, RoundingMode.DOWN).doubleValue();
+    }
+
+    /**
+     * 数值保留n位小数(默认四舍五入)
+     *
+     * @param value 数值
+     * @param n     小数点后保留几位
+     * @return
+     */
+    public static double round(double value, int n) {
+        if (n < 0) n = 0;
+        BigDecimal _value = new BigDecimal(Double.toString(value));
+        BigDecimal one = new BigDecimal("1");
+        return _value.divide(one, n, RoundingMode.HALF_UP).doubleValue();
+    }
+
+}