引用 https://www.yunliantaida.com
需求
- 财务同学需要一个类似execl可自己编写表达式的薪资计算功能,以便可以随时调整算法功能.
调研
MathParser.org-mXparser
示例
<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());
}
ScriptEngine
示例
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class MainTest {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine engine=manager.getEngineByName("javascript");
System.out.println(engine.eval("(3*4)%2+1"));
}
}
网友评论