package com.qmth.boot.tools.excel.convertor.impl;

import com.qmth.boot.tools.excel.convertor.ValueConvertor;
import com.qmth.boot.tools.excel.model.FieldParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;

import java.text.DecimalFormat;

public class FloatConvertor implements ValueConvertor<Float> {

    public static final FloatConvertor instance = new FloatConvertor();

    @Override
    public Float parse(String value, FieldParam param) throws Exception {
        return Float.parseFloat(value);
    }

    @Override
    public void format(Float object, FieldParam param, Cell cell) throws Exception {
        if (StringUtils.isNotBlank(param.getAnnotation().pattern())) {
            cell.setCellValue(new DecimalFormat(param.getAnnotation().pattern()).format(object));
        } else {
            cell.setCellValue(object);
        }
    }
}
