Mark Lalor Mark Lalor - 2 months ago 32
C# Question

Evaluate C# string with math operators

Is there an easy way to evaluate strings like

"(4+8)*2"

So that you'd get the int value of 24?

Or is there a lot of work needed to get this done...?

Answer

Use Ncalc:

Expression e = new Expression("(4+8)*2");
Debug.Assert(24 == e.Evaluate());   

http://ncalc.codeplex.com/

Also, this question had been previously asked and has some interesting answers including Ncalc : c# evaluating string "3*(4+2)" yield int 18