Jelajahi Sumber

1、float截取两位小数值。

jessie 5 tahun lalu
induk
melakukan
2031b09151

+ 7 - 0
src/main/java/com/pavis/ai/app/fda/common/utils/GeneralUtils.java

@@ -13,6 +13,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.math.BigDecimal;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.*;
@@ -25,6 +26,12 @@ import java.util.concurrent.TimeUnit;
  **/
 public class GeneralUtils {
 
+    public static Float toFloatByTwo(Float num){
+        // ROUND_HALF_UP表明四舍五入,ROUND_HALF_DOWN表明五舍六入,2:保留两位小数
+        BigDecimal b = new BigDecimal(num);
+        return b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
+    }
+
     /**
      * json格式判定。
      *

+ 9 - 8
src/main/java/com/pavis/ai/app/fda/service/impl/SearchServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.pavis.ai.app.fda.common.utils.DateUtils;
+import com.pavis.ai.app.fda.common.utils.GeneralUtils;
 import com.pavis.ai.app.fda.form.TestCommon;
 import com.pavis.ai.app.fda.form.follow.Follower;
 import com.pavis.ai.app.fda.form.his.IndustryDetail;
@@ -275,7 +276,7 @@ public class SearchServiceImpl implements SearchService {
             Float value = calLastWeekActual(date,followInfo.getPlateId());
             incInfo = IncInfo.builder()
                     .industryId(followInfo.getPlateId())
-                    .industryValue(value)
+                    .industryValue(GeneralUtils.toFloatByTwo(value))
                     .industryTag(value > 0.0F ? true : false)
                     .industryName(followInfo.getPlateName())
                     .build();
@@ -523,7 +524,7 @@ public class SearchServiceImpl implements SearchService {
             incInfo = IncInfo.builder()
                     .industryId(plate.getPlateId())
                     .industryName(plate.getPlateName())
-                    .industryValue(value)
+                    .industryValue(GeneralUtils.toFloatByTwo(value))
                     .industryTag(industryTag)
                     .build();
             incInfos.add(incInfo);
@@ -625,7 +626,7 @@ public class SearchServiceImpl implements SearchService {
                     // .industryName(plate.getPlateName())
                     // todo 后期需要修改。
                     .industryName(infos.get(0).getPlateName())
-                    .industryValue(value)
+                    .industryValue(GeneralUtils.toFloatByTwo(value))
                     .industryTag(industryTag)
                     .build();
             incInfos.add(incInfo);
@@ -828,7 +829,7 @@ public class SearchServiceImpl implements SearchService {
         Float lastClose = industryPlatesLast.size() > 0 ? null != industryPlatesLast.get(0).getPlateClose() ?
                 industryPlatesLast.get(0).getPlateClose() : 0.0F : 0.0F;
         Float value = close - lastClose;
-        return value;
+        return GeneralUtils.toFloatByTwo(value);
     }
 
     public Float getPredValue(List<IndustryPlate> industryPlates,List<IndustryPlate> industryPlatesLast){
@@ -837,7 +838,7 @@ public class SearchServiceImpl implements SearchService {
         Float lastPred = industryPlatesLast.size() > 0 ? null != industryPlatesLast.get(0).getPlateClosePred() ?
                 industryPlatesLast.get(0).getPlateClosePred() : 0.0F : 0.0F;
         Float value = pred - lastPred;
-        return value;
+        return GeneralUtils.toFloatByTwo(value);
     }
 
     /**
@@ -1021,7 +1022,7 @@ public class SearchServiceImpl implements SearchService {
                     .industryId(industryPlateRes.getPlateId())
                     // .industryName(plate.getPlateName())
                     .industryName(industryPlateRes.getPlateName())
-                    .industryValue(actualVal)
+                    .industryValue(GeneralUtils.toFloatByTwo(actualVal))
                     .industryTag(industryTag)
                     .build();
             incInfos.add(incInfo);
@@ -1046,7 +1047,7 @@ public class SearchServiceImpl implements SearchService {
                     // .industryName(plate.getPlateName())
                     // todo 后期需要修改。
                     .industryName(industryPlateRes.getPlateName())
-                    .industryValue(predVal)
+                    .industryValue(GeneralUtils.toFloatByTwo(predVal))
                     .industryTag(industryTag)
                     .build();
             incInfos.add(incInfo);
@@ -1262,7 +1263,7 @@ public class SearchServiceImpl implements SearchService {
                     // .industryName(plate.getPlateName())
                     // todo 后期需要修改。
                     .industryName(industryPlateRes.getPlateName())
-                    .industryValue(value)
+                    .industryValue(GeneralUtils.toFloatByTwo(value))
                     .industryTag(value > 0.0F ? true : false)
                     .build();
             incInfos.add(incInfo);

+ 5 - 0
src/test/java/com/pavis/ai/app/fda/LocalDateTest.java

@@ -48,6 +48,11 @@ public class LocalDateTest {
     @Autowired
     private IndustryPlateResMapper industryPlateResMapper;
 
+    // 1、实际榜:
+    // 2、预测帮:查找close为空的。
+    // 3、date上周 上上周测试。
+    // 4、数据截取。
+
     @Test
     public void testUpdateIPlateName(){
         // QueryWrapper<IndustryInfo> industryInfoQueryWrapper = new QueryWrapper<>();