JAVA-Math正则工具类

计算工具类

public class MathUtils {

    private static String FLOAT_DIVISION_FORMAT = "%1$0";

    /**
     * 字符串整数加法
     * num1+num2
     * @param  num1   数字1
     * @param  num2   数字2
     * @return String 返回结果
     */
    public static String add(String num1, String num2) {
        return String.valueOf((!RegexUtils.isDigits(num1) ? MagicNumConstant.ZERO : Integer.parseInt(num1)) + (!RegexUtils.isDigits(num2) ? MagicNumConstant.ZERO : Integer.parseInt(num2)));
    }

    /**
     * 字符串整数减法
     * num1 - num2
     * @param  num1    数字1
     * @param  num2    数字2
     * @return String  返回结果
     */
    public static String reduce(String num1, String num2) {
        return String.valueOf((!RegexUtils.isDigits(num1) ? MagicNumConstant.ZERO : Integer.parseInt(num1)) - (!RegexUtils.isDigits(num2) ? MagicNumConstant.ZERO : Integer.parseInt(num2)));
    }

    /**
     *
     * 浮点数除法 num1/num2
     * @param num1     数字1
     * @param num2     数字2
     * @param decimal  结果小数位数
     * @return Float   返回结果
     */
    public static Float floatDivision(String num1, String num2, Integer decimal) {
        if (!RegexUtils.isFloat(num1) || !RegexUtils.isFloat(num2)) {
            return null;
        }
        if (Float.valueOf(num2).equals(0f)) {
            return null;
        }
        if (decimal != null && decimal > MagicNumConstant.ZERO) {
            Integer d = Integer.parseInt(MagicNumConstant.ONE + String.format(FLOAT_DIVISION_FORMAT + decimal + "d", MagicNumConstant.ZERO));
            return (float) (Math.round((Float.parseFloat(num1) / Float.parseFloat(num2)) * d)) / d;
        } else {
            return Float.parseFloat(num1) / Float.parseFloat(num2);
        }
    }



}
正则匹配工具类
public class RegexUtils {
    private static final String DIGIT = "^[0-9]*$";
    private static final String FLOAT = "^[-+]?[0-9]*\\.?[0-9]+$";
    /**
     * str待匹配文本
     * regex 正则表达式
     *返回str中匹配regex的第一个子串
     */
    public static String getMatcher(String str,String regex) {
        try{
            if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)){
                return "";
            }
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = p.matcher(str);
            matcher.find();
            return matcher.group();
        }catch (IllegalStateException e){
            log.error(e.getMessage(), e);
            return "";
        }
    }

    /**
     * 数字匹配
     * @param str
     * @return
     */
    public static boolean isDigits(String str){
        if (StringUtils.isEmpty(str)){
            return false;
        }
        return str.matches(DIGIT);
    }

    /**
     * 浮点数匹配
     * @param str
     * @return
     */
    public static boolean isFloat(String str){
        if (StringUtils.isEmpty(str)){
            return false;
        }
        return str.matches(FLOAT);
    }

    /**
     * 校验字符串是否是json格式
     *
     * @param json json字符串
     * @return 校验结果 true:是 false:否
     */
    public static boolean isJson(String json) {
        boolean result = false;
        try {
            if(!StringUtils.isEmpty(json)){
                JSON.parse(json);
                result = true;
            }
        } catch (Exception e) {
            result=false;
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/caryeko/article/details/141320711