package com.bohuikeji.frame.module.admin.serializer;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * @author ：mmzs
 * @date ：Created in 2021/2/26 15:45
 * @description：Decimal格式化保留俩位小数
 * @modified By：
 * @version: 1$
 */
public class Decimal2Serializer implements ObjectSerializer {
    @Override
    public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException {
        if(o!=null){
            SerializeWriter out = jsonSerializer.out;
            BigDecimal bigDecimal = new BigDecimal(o.toString()).setScale(2,BigDecimal.ROUND_DOWN);
            out.writeString(bigDecimal.toPlainString());
        }
    }
}
