Java 字符串计算表达式转公式获取计算结果
作者:互联网
在 Java 中计算字符串数值表达式可以用 javax.script.ScriptEngine#eval(java.lang.String),通过调用 JavaScript 来计算
** 上代码 **
public static void main(String[] args) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
String expression = "0.032*2017-58.64";
try {
String result = String.valueOf(scriptEngine.eval(expression));
System.out.println(result);
} catch (ScriptException e) {
e.printStackTrace();
}
}
// 输出结果:5.903999999999996
标签:Java,计算结果,String,scriptEngine,result,eval,表达式,ScriptEngineManager 来源: https://blog.csdn.net/u012156165/article/details/111593897