Java Script evaluateExpression(String s)

Here you can find the source of evaluateExpression(String s)

Description

Converts a string to a mathematical expression and evaluates it with Javascript.

License

Open Source License

Parameter

Parameter Description
s String form of a mathmatical expression. Must be able to evaluate in Javascript

Exception

Parameter Description
ScriptException If the expression is unparseable

Return

the result

Declaration

public static double evaluateExpression(String s) throws ScriptException 

Method Source Code

//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;
    }
}

Related

  1. escapeJavascript(String str)
  2. eval(String expression, Object... arguments)
  3. eval(String name, Map params)
  4. eval(String script)
  5. evaluateExpression(String expr)
  6. executeAppleScript(String script)
  7. executeScript(String script)
  8. executeScript(String strScript)
  9. fillStringCollection(ScriptObjectMirror som, Collection collection)