工做遇到業務計算,規則常常變更,考慮配置數學表達式java
<dependency> <groupId>org.mariuszgromada.math</groupId> <artifactId>MathParser.org-mXparser</artifactId> <version>4.2.0</version> </dependency>
public static void main(String[] args) throws InterruptedException { Argument days = new Argument("days = 30"); Expression min = new Expression("min(days/10,1)", days); mXparser.consolePrint(min.getExpressionString() + "----" + min.calculate()); Expression e = new Expression("2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4"); mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate()); Expression abs = new Expression("abs(-1)"); mXparser.consolePrintln("Res: " + abs.getExpressionString() + " = " + abs.calculate()); Expression sqrt = new Expression("sqrt(2)"); mXparser.consolePrintln("Res: " + sqrt.getExpressionString() + " = " + sqrt.calculate()); }
結果:[mXparser-v.4.2.0] min(days/10,1)----1.0 express
http://mathparser.org/mxparser-tutorial/simple-expressions/spa