From d50fa4085b34bd4fe2eca55ea93853f50138108d Mon Sep 17 00:00:00 2001 From: cabbage <281119120@qq.com> Date: Fri, 13 Jun 2025 13:59:48 +0800 Subject: [PATCH] =?UTF-8?q?##=20=E4=BD=BF=E7=94=A8BigDecimalFormat?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=EF=BC=8C=E9=BB=98=E8=AE=A4=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E5=9B=9B=E8=88=8D=E4=BA=94=E5=85=A5=E6=94=B9=E4=B8=BA=E5=8E=BB?= =?UTF-8?q?=E5=B0=BE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/config/BigDecimalSerializer.java | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/bd-common/bd-common-core/src/main/java/com/hzs/common/core/config/BigDecimalSerializer.java b/bd-common/bd-common-core/src/main/java/com/hzs/common/core/config/BigDecimalSerializer.java index 37378e8e..18f9bdc8 100644 --- a/bd-common/bd-common-core/src/main/java/com/hzs/common/core/config/BigDecimalSerializer.java +++ b/bd-common/bd-common-core/src/main/java/com/hzs/common/core/config/BigDecimalSerializer.java @@ -1,6 +1,5 @@ package com.hzs.common.core.config; -import cn.hutool.json.JSONUtil; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.JsonMappingException; @@ -8,14 +7,11 @@ import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.hzs.common.core.annotation.BigDecimalFormat; -import com.hzs.common.core.constant.CacheConstants; -import com.hzs.common.core.context.SecurityContextHolder; -import com.hzs.common.core.service.RedisService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.jackson.JsonComponent; import java.io.IOException; import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Objects; @@ -28,15 +24,6 @@ public class BigDecimalSerializer extends JsonSerializer implements // 默认保留2位小数 private String format = "#0.00"; - private String zeroFormat = "#0"; - - private RedisService redisService; - - @Autowired - public void setRedisService(RedisService redisService) { - this.redisService = redisService; - } - /** * 序列化处理方式 * @@ -47,12 +34,10 @@ public class BigDecimalSerializer extends JsonSerializer implements */ @Override public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { -// if(ComputeUtil.compareEqual(ComputeUtil.computeSubtract(bigDecimal, bigDecimal.setScale(0, RoundingMode.DOWN)), BigDecimal.ZERO)){ -// jsonGenerator.writeString(new DecimalFormat(zeroFormat).format(bigDecimal)); -// }else{ -// jsonGenerator.writeString(bigDecimal.setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toString()); - jsonGenerator.writeString(new DecimalFormat(format).format(bigDecimal)); -// } + DecimalFormat decimalFormat = new DecimalFormat(format); + // 改为使用去尾方式显示 + decimalFormat.setRoundingMode(RoundingMode.DOWN); + jsonGenerator.writeString(decimalFormat.format(bigDecimal)); } /** @@ -66,15 +51,13 @@ public class BigDecimalSerializer extends JsonSerializer implements @Override public JsonSerializer createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { if (beanProperty != null) { - Object bdCountry = redisService.getCacheObject(CacheConstants.BD_COUNTRY + SecurityContextHolder.getUserCountry()); if (Objects.equals(beanProperty.getType().getRawClass(), BigDecimal.class)) { BigDecimalFormat bigDecimalFormat = beanProperty.getAnnotation((BigDecimalFormat.class)); if (bigDecimalFormat == null) { bigDecimalFormat = beanProperty.getContextAnnotation(BigDecimalFormat.class); } BigDecimalSerializer bigDecimalSerializer = new BigDecimalSerializer(); - int numberPlaces = Integer.parseInt(JSONUtil.parseObj(bdCountry).get("numberPlaces").toString()); - bigDecimalSerializer.format = replaceNumber(numberPlaces); + bigDecimalSerializer.format = replaceNumber(); if (bigDecimalFormat != null) { bigDecimalSerializer.format = bigDecimalFormat.value(); } @@ -85,11 +68,12 @@ public class BigDecimalSerializer extends JsonSerializer implements return new BigDecimalSerializer(); } - private String replaceNumber(int numberPlaces) { + private String replaceNumber() { StringBuilder number = new StringBuilder("#0."); - for (int i = 0; i < numberPlaces; i++) { + for (int i = 0; i < 6; i++) { number.append("0"); } return number.toString(); } + }