Is it possible to solve a math expression given in a String?

1
Hi all,  If i'm having the following String: ‘(8+6+4+2+10)/5’, then how would I be able to solve this expression.  It is not possible to parse this expression to decimal ofcourse, so what would be a good way to solve this? Best to use a java action for this or is there another solution?  
asked
1 answers
2

IOP easiest is to use the javascript eval function

//The eval() function evaluates JavaScript code represented as a string.

console.log(eval('2 + 2'));
// expected output: 4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

 

If you want to do this in java you can use the built in javascript engine see this answer on Stackoverflow: 

https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form

Hope this helps,

Andrej

answered