package com.zcxk.charge.web.util.jsonserializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @description BigDecimal保留三位小数
 * @author  wangli
 * @data 2020/12/31 14:17
 */
public class BigDecimalJsonSerializer extends JsonSerializer<BigDecimal> {

    private DecimalFormat df = new DecimalFormat("0.000");

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(bigDecimal != null){
            jsonGenerator.writeString(df.format(bigDecimal));
        }
    }
}
