java表达式的字符串运算出结果

来源:互联网 发布:永辉渔具淘宝 编辑:程序博客网 时间:2024/03/29 20:21

首先在项目中添加或应用bsh工具包

1.官网下载:http://www.beanshell.org/download.html

2.本人的资源:http://download.csdn.net/detail/qq_30552993/9610435

/*** * @param  exp 算数表达式 * @return 根据表达式返回结果 */private String getRs(String exp){<span style="white-space:pre"></span>Interpreter bsh = new Interpreter();<span style="white-space:pre"></span>Number result = null;<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>exp = filterExp(exp);<span style="white-space:pre"></span>result = (Number)bsh.eval(exp);<span style="white-space:pre"></span>} catch (EvalError e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>return "算数公式错误";<span style="white-space:pre"></span>}        <span style="white-space:pre"></span>exp = result.doubleValue()+"";<span style="white-space:pre"></span>if(exp.endsWith(".0"))<span style="white-space:pre"></span>exp = exp.substring(0, exp.indexOf(".0"));<span style="white-space:pre"></span>return exp;}

0 0