This question already has an answer here:
Is there a way in Java to get the result from this mathematical expression:
String code = "5+4*(7-15)";
In other hand what's the best way to parse an arithmetic expression?
This question already has an answer here:
Is there a way in Java to get the result from this mathematical expression:
String code = "5+4*(7-15)";
In other hand what's the best way to parse an arithmetic expression?
i recently developed a expression parser and released it under the apache license. you can grab it at http://projects.congrace.de/exp4j/index.html
hope that helped
Recently I was using very mature math expression parser library, open source, giving the same API for JAVA and .NET. The library name is mXparser. mXparser provides basic functionalities (simple formulas parsing and calculation) and more advanced ones (i.e. user defined arguments, functions). Additionally it is worth to notice that mXparser has rich built-in math collection (meaning operators, unary / binary / variadic functions, iterated operators such as summation and product).
http://mathparser.org/
http://mathparser.org/mxparser-tutorial/
Please find below a few examples to have more clear view on the syntax.
Example 1 - simple formula
Example 2 - built-in function
Example 3 - built-in constants
Example 4 - user defined arguments and constants
Example 5 - user defined functions
Example 6 - user defined recursion
Best regards,
LK
You can use the ScriptEngine class and evaluate it as a javascript string
Indeed , yu should know that the result of the following instruction in javascript :
There may be a better way, but this one works.