Here you can find the source of evaluateExpression(String s)
Parameter | Description |
---|---|
s | String form of a mathmatical expression. Must be able to evaluate in Javascript |
Parameter | Description |
---|---|
ScriptException | If the expression is unparseable |
public static double evaluateExpression(String s) throws ScriptException
//package com.java2s; //License from project: Open Source License import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Main { /**/*from w ww . ja v a 2 s . c o m*/ * Converts a string to a mathematical expression and evaluates it with * Javascript. * * @param s * String form of a mathmatical expression. Must be able to * evaluate in Javascript * @return the result * @throws ScriptException * If the expression is unparseable */ public static double evaluateExpression(String s) throws ScriptException { final ScriptEngineManager mgr = new ScriptEngineManager(); final ScriptEngine engine = mgr.getEngineByName("JavaScript"); final double result = Double.valueOf("" + engine.eval(s)); return result; } }