C# Math calculator [duplicate]

DataTable has a Compute method that allows you to write this:

var result = new DataTable().Compute("2-3/4*12", null);

Note that this is limited to simple math expressions.

Other option consist in using a dynamic language in the DLR such as IronPython and IronRuby. Check-out this post:

var engine = new IronPython.Hosting.PythonEngine();
double result = pythonEngine.EvaluateAs<double>("2-3/4*12");

You may also check the NCalc library on GitHub.

Leave a Comment