中央热水项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1022 B

package com.mh.user.utils;
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;
/**
* 小数保留2位返回给前端序列化器
*
* @author: nxr
* date: 2023-03-30
*/
public class Double2Serializer extends JsonSerializer<Double> {
private DecimalFormat df = new DecimalFormat("0.0000");
/**
* 小数保留2位返回给前端序列化器
* @param data
* @param jsonGenerator
* @param serializerProvider
* @throws IOException
*/
@Override
public void serialize(Double data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (data != null) {
if (data == 0) {
jsonGenerator.writeString("0");
} else {
jsonGenerator.writeString(df.format(data));
}
}
}
}