Browse Source

modify:

1. 金额全局格式化为四舍五入两位小数;
tags/24082201
WendyYang 4 months ago
parent
commit
20ac35008c
1 changed files with 10 additions and 0 deletions
  1. +10
    -0
      hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapter.java

+ 10
- 0
hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapter.java View File

@@ -1,6 +1,7 @@
package com.hz.pm.api.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.hz.pm.api.common.interceptor.LogInterceptor;
@@ -12,6 +13,8 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -32,6 +35,13 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
config.getSerializeConfig().addFilter(BigDecimal.class, (ValueFilter) (object, name, value) -> {
if (!(value instanceof BigDecimal)) {
return value;
}
// 保留两位小数
return ((BigDecimal) value).setScale(2, RoundingMode.HALF_UP).toPlainString();
});
converter.setFastJsonConfig(config);
converter.setSupportedMediaTypes(supportMediaTypeList);
converter.setDefaultCharset(StandardCharsets.UTF_8);


Loading…
Cancel
Save