package com.zcxk.core.utils.converter;

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

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

public class Double2Serializer extends JsonSerializer<Double> {

    private DecimalFormat df = new DecimalFormat("0.##");

    /**
     * 小数保留3位返回给前端序列化器
     * @param data
     * @param jsonGenerator
     * @param serializerProvider
     * @throws IOException
     */
    @Override
    public void serialize(Double data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        if (data != null) {
            jsonGenerator.writeNumber(df.format(data));
        }
    }

}