DigitalUtils.java 5.5 KB
package com.skua.tool.util;

import org.apache.commons.lang3.StringUtils;
import org.bytedeco.libfreenect2.Logger;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * 数字工具类
 * </pre>
 *
 * @author sonin
 * @version 1.0 2022/4/20 17:27
 */
public class DigitalUtils {

    /**
     * 计算保留n位小数
     *
     * @return
     */
    public static String nPoint(Object src, int n) {
        if (src == null) {
            return String.format("%." + n + "f", 0D);
        } else if (src instanceof Double) {
            return String.format("%." + n + "f", src);
        } else if (src instanceof String) {
            if (isNumeric(src.toString())) {
                return String.format("%." + n + "f", Double.parseDouble("" + src));
            } else {
                return String.format("%." + n + "f", 0D);
            }
        }
        return "" + src;
    }

    /**
     * 加法 保留两位小数
     * @return
     */
    public static String add(String val1 , String  val2){
        BigDecimal result   = BigDecimal.ZERO;
        if(StringUtils.isNotEmpty(val1)){
            result =   new BigDecimal(val1);
            // BigDecimal rounded = original.multiply(  num2).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if(StringUtils.isNotEmpty(val2)){
            BigDecimal num2 = new BigDecimal(val2);
            result = result.add(  num2  );
            result = result.multiply(  num2).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return result.toString();
    }


    /**
     * 乘法 保留两位小数
     * @return
     */
    public static String multiply(String multiplier , String  multiplicand){
        String resultStr = "0";
        if(multiplier != null ){
            BigDecimal original = new BigDecimal(multiplier);
            BigDecimal num2 = new BigDecimal(multiplicand);
           // BigDecimal rounded = original.multiply(  num2).setScale(2, BigDecimal.ROUND_HALF_UP);
            resultStr = original.multiply(  num2  ).toString();
        }
        return resultStr;
    }

    public static String subtract(String num1 , String  num2){
        String resultStr = null ;
        if(StringUtils.isNotEmpty(num1) &&  StringUtils.isNotEmpty(num2) ){
            BigDecimal num1_b = new BigDecimal(num1);
            BigDecimal num2_b = new BigDecimal(num2);
            // BigDecimal rounded = original.multiply(  num2).setScale(2, BigDecimal.ROUND_HALF_UP);
            resultStr = num1_b.subtract(  num2_b  ).toString();
        }
        return resultStr;
    }

    /***
     * 比较两个数
     * @param num1
     * @param num2
     * @return  comparisonResult=0 相等; 大于返回1 ,小于返回-1
     */
    public static Integer compareTo(String num1 , String  num2){
        Integer  comparisonResult  = null ;
        if(StringUtils.isNotEmpty(num1) &&  StringUtils.isNotEmpty(num2) ){
            BigDecimal num1_b = new BigDecimal(num1);
            BigDecimal num2_b = new BigDecimal(num2);
            // BigDecimal rounded = original.multiply(  num2).setScale(2, BigDecimal.ROUND_HALF_UP);
            comparisonResult  =  num1_b.compareTo(  num2_b  );
        }
        return comparisonResult ;
    }


    /**
     * 除法 保留两位小数
     * @param dividend 请输入被除数
     * @param divisor  请输入除数
     * @return
     */
    public static String division(String dividend , String  divisor ){
        String resultStr = null;
        String expression = dividend+ "/ "+ divisor;
        resultStr = JSUtils.executeExpression(expression,"0.00");
       /* try{
            if( isNumber(dividend) && isNumber( divisor) && Double.parseDouble(divisor) != 0  ){
                BigDecimal   dividend_BD = new BigDecimal(dividend);
                BigDecimal   divisor_BD = new BigDecimal(divisor);
                BigDecimal result = dividend_BD.divide(divisor_BD).setScale(2, RoundingMode.HALF_UP);
                resultStr = result.toString();
            }
        }catch(Exception e){
            System.out.println("dividend = "+ dividend +";  divisor = "+  divisor);
            e.printStackTrace();
        }*/

        //System.out.print("请输入被除数:");double dividend =scanner.nextDouble();
       // System.out.print("请输入除数:");double divisor =scanner.nextDouble();

        return resultStr;
    }

    /**
     * 判断字符串是否是数字类型
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if(org.apache.commons.lang.StringUtils.isNotEmpty(str) ){
            Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]*");
            // 科学计数法
            Pattern pattern2 = Pattern.compile("^[+-]?[\\d]+([.][\\d]*)?([Ee][+-]?[\\d]+)?$");
            if (StringUtils.isEmpty(str)) {
                return false;
            } else {
                Matcher isNum = pattern.matcher(str);
                Matcher isNum2 = pattern2.matcher(str);
                return isNum.matches() || isNum2.matches();
            }
        }
        return false;
    }


    public static boolean isNumber(String str) {
        // Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        if(org.apache.commons.lang.StringUtils.isNotEmpty(str) ){
            String reg = "^(\\-|\\+)?\\d+(\\.\\d+)?$";
            Pattern pattern = Pattern.compile(reg);
            Matcher match = pattern.matcher(str);
            return match.matches();
        }
        return  false;
    }

}