Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert mathematical string to int

Is there an easy way to take a String such as "5*4" and return 20?

like image 429
George Avatar asked Jul 15 '10 06:07

George


1 Answers

The most simple way would be to use the Rhino JavaScript engine available with the JRE 6 standard API.

Edit: as per the comments, if the strings are user-supplied, this could be a potential security hole. Be sure to filter out everything except digits, braces and mathematical operators.

like image 50
Michael Borgwardt Avatar answered Oct 09 '22 06:10

Michael Borgwardt