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 DoubleConvertor implements ValueConvertor<Double> {

    public static final DoubleConvertor instance = new DoubleConvertor();

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

    @Override
    public void format(Double 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);
        }
    }
}
