首页 > 其他分享 >BigDecimalSerializer

BigDecimalSerializer

时间:2024-03-03 22:00:49浏览次数:19  
标签:BigDecimal numberFormat value class BigDecimalSerializer public

@JsonSerialize(using = BigDecimalSerializer.class)

public class BigDecimalSerializer extends JsonSerializer {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
BigDecimal _value = value;
if (value == null) {
_value = new BigDecimal(0);
}

    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(8);
    numberFormat.setMinimumFractionDigits(_value.scale());
    numberFormat.setGroupingUsed(false);
    numberFormat.setRoundingMode(RoundingMode.FLOOR);
    gen.writeString(numberFormat.format(_value));
}

}

标签:BigDecimal,numberFormat,value,class,BigDecimalSerializer,public
From: https://www.cnblogs.com/youkiko/p/18050839

相关文章