eval() should be adequate, but I'd be wary of it. There is no other built-in solution, though. Just for kicks, I threw together a really simple parser for that sort of arithmetic expression, in JavaScript. Full source here: http://gist.github.com/332477
Basic mechanism is to split on each operator, in order of precedence low->high, and then recursively evaluate each chunk, with a parseInt at the base level, and combine with a simple array reduction on the results, using the operator from the split. Here's the core function (sum, negasum, dividend, product are just array reduce functions for each operator):
calc = function(input) {
if (input.indexOf("+") >= 0) {
return sum(input.split("+"));
} else if (input.indexOf("-") >= 0) {
return negasum(input.split("-"));
} else if (input.indexOf("*") >= 0) {
return product(input.split("*"));
} else if (input.indexOf("/") >= 0) {
return dividend(input.split("/"));
} else {
return parseInt(input, 10);
}
};
Only supports positive integers, doesn't support parens.
Results for your example (from the console):
calc("2+3*4/5") -> 4.4
eval("2+3*4/5") -> 4.4