Explorar el Código

优化 小数点位数处理

wuxw hace 3 años
padre
commit
76f0155e64

+ 18 - 17
java110-bean/src/main/java/com/java110/vo/api/feeConfig/ApiFeeConfigDataVo.java

@@ -12,8 +12,8 @@ public class ApiFeeConfigDataVo implements Serializable {
     private String endTime;
     private String computingFormula;
     private String computingFormulaName;
-    private String squarePrice;
-    private String additionalAmount;
+    private Double squarePrice;
+    private Double additionalAmount;
     private String feeTypeCdName;
     private String feeFlagName;
     private String isDefault;
@@ -91,21 +91,6 @@ public class ApiFeeConfigDataVo implements Serializable {
         this.computingFormula = computingFormula;
     }
 
-    public String getSquarePrice() {
-        return squarePrice;
-    }
-
-    public void setSquarePrice(String squarePrice) {
-        this.squarePrice = squarePrice;
-    }
-
-    public String getAdditionalAmount() {
-        return additionalAmount;
-    }
-
-    public void setAdditionalAmount(String additionalAmount) {
-        this.additionalAmount = additionalAmount;
-    }
 
     public String getFeeTypeCdName() {
         return feeTypeCdName;
@@ -215,4 +200,20 @@ public class ApiFeeConfigDataVo implements Serializable {
     public void setUnits(String units) {
         this.units = units;
     }
+
+    public Double getSquarePrice() {
+        return squarePrice;
+    }
+
+    public void setSquarePrice(Double squarePrice) {
+        this.squarePrice = squarePrice;
+    }
+
+    public Double getAdditionalAmount() {
+        return additionalAmount;
+    }
+
+    public void setAdditionalAmount(Double additionalAmount) {
+        this.additionalAmount = additionalAmount;
+    }
 }

+ 38 - 0
java110-utils/src/main/java/com/java110/utils/util/MoneyUtil.java

@@ -0,0 +1,38 @@
+package com.java110.utils.util;
+
+import java.math.BigDecimal;
+
+public class MoneyUtil {
+
+    /**
+     *  <option value="1">{{vc.i18n('四舍五入','editFeeConfig')}}</option>
+     <option value="3">{{vc.i18n('向上进位','editFeeConfig')}}</option>
+     <option value="4">{{vc.i18n('向下进位','editFeeConfig')}}</option>
+     */
+
+    public static final String HALF_UP = "1";
+    public static final String UP = "3";
+    public static final String DOWN = "4";
+
+    /**
+     * 四舍五入
+     * @param price
+     * @param scale
+     * @param decimalPlace
+     * @return
+     */
+    public static double computePriceScale(double price,String scale,int decimalPlace){
+
+        BigDecimal feeTotalPrice = new BigDecimal(price);
+
+        if(DOWN.equals(scale)) {
+            feeTotalPrice = feeTotalPrice.setScale(decimalPlace, BigDecimal.ROUND_DOWN);
+        }else if(UP.equals(scale)){
+            feeTotalPrice = feeTotalPrice.setScale(decimalPlace, BigDecimal.ROUND_UP);
+        }else{
+            feeTotalPrice = feeTotalPrice.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
+        }
+
+        return feeTotalPrice.doubleValue();
+    }
+}

+ 11 - 11
service-fee/src/main/java/com/java110/fee/cmd/fee/ListFeeCmd.java

@@ -26,10 +26,7 @@ import com.java110.intf.user.IOwnerCarInnerServiceSMO;
 import com.java110.intf.user.IOwnerRoomRelV1InnerServiceSMO;
 import com.java110.utils.cache.MappingCache;
 import com.java110.utils.exception.CmdException;
-import com.java110.utils.util.Assert;
-import com.java110.utils.util.BeanConvertUtil;
-import com.java110.utils.util.DateUtil;
-import com.java110.utils.util.StringUtil;
+import com.java110.utils.util.*;
 import com.java110.vo.api.fee.ApiFeeDataVo;
 import com.java110.vo.api.fee.ApiFeeVo;
 import org.slf4j.Logger;
@@ -280,13 +277,15 @@ public class ListFeeCmd extends Cmd {
      */
     private void computeFeePriceByRoom(FeeDto feeDto, double oweMonth) {
         String computingFormula = feeDto.getComputingFormula();
-        DecimalFormat df = new DecimalFormat("0.00");
         Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDto);
         feeDto.setFeePrice(Double.parseDouble(feePriceAll.get("feePrice").toString()));
-        feeDto.setFeeTotalPrice(Double.parseDouble(feePriceAll.get("feeTotalPrice").toString()));
+        //BigDecimal feeTotalPrice = new BigDecimal(Double.parseDouble(feePriceAll.get("feeTotalPrice").toString()));
+        feeDto.setFeeTotalPrice(MoneyUtil.computePriceScale(Double.parseDouble(feePriceAll.get("feeTotalPrice").toString()),
+                feeDto.getScale(),
+                Integer.parseInt(feeDto.getDecimalPlace())));
         BigDecimal curFeePrice = new BigDecimal(feeDto.getFeePrice());
         curFeePrice = curFeePrice.multiply(new BigDecimal(oweMonth));
-        feeDto.setAmountOwed((df.format(curFeePrice)));
+        feeDto.setAmountOwed(MoneyUtil.computePriceScale(curFeePrice.doubleValue(), feeDto.getScale(), Integer.parseInt(feeDto.getDecimalPlace())) + "");
         //动态费用
         if ("4004".equals(computingFormula)
                 && FeeDto.FEE_FLAG_ONCE.equals(feeDto.getFeeFlag())
@@ -308,18 +307,19 @@ public class ListFeeCmd extends Cmd {
      */
     private void computeFeePriceByContract(FeeDto feeDto, double oweMonth) {
         String computingFormula = feeDto.getComputingFormula();
-        DecimalFormat df = new DecimalFormat("0.00");
         Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDto);
         feeDto.setFeePrice(Double.parseDouble(feePriceAll.get("feePrice").toString()));
-        feeDto.setFeeTotalPrice(Double.parseDouble(feePriceAll.get("feeTotalPrice").toString()));
+        feeDto.setFeeTotalPrice(MoneyUtil.computePriceScale(Double.parseDouble(feePriceAll.get("feeTotalPrice").toString()),
+                feeDto.getScale(),
+                Integer.parseInt(feeDto.getDecimalPlace())));
         BigDecimal curFeePrice = new BigDecimal(feeDto.getFeePrice());
         curFeePrice = curFeePrice.multiply(new BigDecimal(oweMonth));
-        feeDto.setAmountOwed(df.format(curFeePrice));
+        feeDto.setAmountOwed(MoneyUtil.computePriceScale(curFeePrice.doubleValue(), feeDto.getScale(), Integer.parseInt(feeDto.getDecimalPlace())) + "");
         //动态费用
         if ("4004".equals(computingFormula)
                 && FeeDto.FEE_FLAG_ONCE.equals(feeDto.getFeeFlag())
                 && !FeeDto.STATE_FINISH.equals(feeDto.getState())) {
-            feeDto.setAmountOwed(df.format(curFeePrice) + "");
+            feeDto.setAmountOwed(MoneyUtil.computePriceScale(curFeePrice.doubleValue(), feeDto.getScale(), Integer.parseInt(feeDto.getDecimalPlace())) + "");
             feeDto.setDeadlineTime(DateUtil.getCurrentDate());
         }
         //考虑租金递增

+ 1 - 1
springboot/src/test/java/com/java110/AppTest.java

@@ -24,7 +24,7 @@ public class AppTest
     public void shouldAnswerWithTrue()
     {
 
-        double amount = Double.parseDouble("0.6666");
+        double amount = Double.parseDouble("0.6600");
         System.out.println(amount);
 
         BigDecimal amountBig = new BigDecimal(amount);