import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* 数学工具
*
* @version 1.0
* @date 2022/6/6 18:01
* @since : JDK 11
*/
public class NumUtils {
/**
* 注意:仅支持评估简单数学逻辑表达式(例如:" 3 > 1")
*
* @param symbol 判断符号
* @param n1 比较值
* @param n2 比较基准,被比较值(阈值)
* @return
* @throws ScriptException
*/
@SuppressWarnings("all")
public static boolean expressionMathematicalEvaluation(String symbol, String numberStr, String baseNumberStr) {
Boolean result = false;
try {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine javaScript = scriptEngineManager.getEngineByName("JavaScript");
String expression = new StringBuffer()
.append(numberStr)
.append(" ")
.append(symbol)
.append(" ")
.append(baseNumberStr).toString();
result = (Boolean) javaScript.eval(expression);
} catch (ScriptException e) {
e.printStackTrace();
result = false;
}
return result;
}
}