package com.zcxk.core.utils.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保留两位小数
 * @auto wangli
 * @data 2020/12/31 14:17
 */
public class BigDecimalJsonSerializerTwo extends JsonSerializer<BigDecimal> {

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

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