From dc4d67fcf8a0305b5e046c2bbc17c6944b4c4a3a Mon Sep 17 00:00:00 2001 From: Gauthier LO Date: Tue, 22 Aug 2023 17:34:03 +0200 Subject: [PATCH] fix: shipping fee calculation precision --- .../domain/logistic/CostTrialCalculation.java | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/logistic/CostTrialCalculation.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/logistic/CostTrialCalculation.java index dbc57153b..2100661e7 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/logistic/CostTrialCalculation.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/logistic/CostTrialCalculation.java @@ -6,6 +6,7 @@ import lombok.Data; import org.jeecg.modules.business.entity.LogisticChannelPrice; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Date; /** @@ -23,45 +24,34 @@ public class CostTrialCalculation { @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") private final Date effectiveDate; - private final double unitPrice; + private final BigDecimal unitPrice; - private final double shippingCost; + private final BigDecimal shippingCost; - private final double registrationCost; + private final BigDecimal registrationCost; - private final double additionalCost; + private final BigDecimal additionalCost; - private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode, double unitPrice, double shippingCost, - double registrationCost, double additionalCost, Date effectiveDate) { + private CostTrialCalculation(String countryCode, String logisticsChannelName, String logisticChannelCode, BigDecimal unitPrice, BigDecimal shippingCost, + BigDecimal registrationCost, BigDecimal additionalCost, Date effectiveDate) { this.countryCode = countryCode; this.logisticsChannelName = logisticsChannelName; this.logisticChannelCode = logisticChannelCode; this.unitPrice = unitPrice; - this.shippingCost = format(shippingCost); - this.registrationCost = format(registrationCost); - this.additionalCost = format(additionalCost); + this.shippingCost = shippingCost; + this.registrationCost = registrationCost; + this.additionalCost = additionalCost; this.effectiveDate = effectiveDate; } public CostTrialCalculation(LogisticChannelPrice price, int weight, String logisticsChannelName, String code) { - this(price.getEffectiveCountry(), logisticsChannelName, code, price.getCalUnitPrice().doubleValue(), price.calculateShippingPrice(BigDecimal.valueOf(weight)).doubleValue(), - price.getRegistrationFee().doubleValue(), price.getAdditionalCost().doubleValue(), price.getEffectiveDate()); + this(price.getEffectiveCountry(), logisticsChannelName, code, price.getCalUnitPrice(), price.calculateShippingPrice(BigDecimal.valueOf(weight)), + price.getRegistrationFee(), price.getAdditionalCost(), price.getEffectiveDate()); } @JsonProperty("TotalCost") public double getTotalCost() { - return format(shippingCost + registrationCost + additionalCost); - } - - - /** - * Format a decimal with 2 unit. - * - * @param n the number to format - * @return the number formatted - */ - private double format(double n) { - return BigDecimal.valueOf(n).setScale(2, BigDecimal.ROUND_UP).doubleValue(); + return shippingCost.add(registrationCost).add(additionalCost).setScale(2, RoundingMode.CEILING).doubleValue(); } }