package com.ruoyi.caltools.controller;

import java.text.DecimalFormat;
import java.util.Optional;

public class FormatUtil {
    private static final DecimalFormat df1 = new DecimalFormat("#0.0");
    private static final DecimalFormat df2 = new DecimalFormat("#0.00");
    private static final DecimalFormat df3 = new DecimalFormat("#0.000");
    private static final DecimalFormat df4 = new DecimalFormat("#0.0000");
    private static final DecimalFormat df5 = new DecimalFormat("#0.00000");

    public static double format(double value, Optional<Integer> weishu)  {
        double result;
        Integer weishudefault=weishu.orElse(4);

        switch (weishudefault) {
            case 1:
                result= Double.parseDouble(df1.format(value));
            break;
            case 2:
                result= Double.parseDouble(df2.format(value));
            break;
            case 3:
                result= Double.parseDouble(df3.format(value));
            break;
            case 4:
                result= Double.parseDouble(df4.format(value));
            break;
            case 5:
                result= Double.parseDouble(df5.format(value));
            break;

            default:
                throw new IllegalStateException("Unexpected value: " + weishu);
        }
        return    result;
    }

}