Looking for something like
int Result;
DataTable dt2 = new DataTable();
var v = dt2.Compute("3+2-34*12", "");
Result=Convert.ToInt32(v);
the code above, which solves the text base formula. Unfortunately, the code above only works for some basic operators (+,-,/,*). Need a little more complex one (like squareroot, ^ at least).
Could you help me to find something tosolve for a little more complex equations?
You can use Roslyn scripting API for that. Add Microsoft.CodeAnalysis.CSharp.Scripting package and evaluate C# code like this:
static async Task<double> EvaluateFormulaAsync(string formula)
{
return await CSharpScript.EvaluateAsync<double>(formula,
ScriptOptions.Default.WithImports("System.Math"));
}
Usage:
var result = EvaluateFormulaAsync("Sqrt(2) + 2 * 15").Result; // 31.4142135623731
Note: Scripting API requires .NET Framework 4.6+ or .NET Core 1.1