package com.imdroid.common.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Layton
 * @date 2023/2/19 20:17
 */
public class NumberUtils {

    public static Double scaleTwo(String str) {
        return BigDecimal.valueOf(Double.parseDouble(str)).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static Double scaleTwo(Double value) {
        return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static Double scale(Double value, int scale) {
        return BigDecimal.valueOf(value).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }

    public static String removeBrackets(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        return str.replaceAll("\\[", "").replaceAll("]", "");
    }
}
