This problem is encountered in the project, giving a expression, the result is similar to the eval in JavaScript. Colleagues use this method to solve, feel very clever, special affixed to this.
Using the features of the DataTable-compatible SQL formula in Ado.net, it is now concluded that as long as it is a simple formula that can be supported in SQL, the value can be obtained in this way.
Using System;
Using System.Collections;
Public class MyClass
{
public static void Main ()
{
& nbsp; try
{
Double dblresult = convert.todouble (Myclass.eval (@ "1+2*3+120%50/4"));
Console.Write (Dblresult);
console.readline ();
}
catch (System.Exception ex)
{
Console.WriteLine (ex. message);
console.readline ();
}
}
public static object Eval (string expression)
{
System.Data.DataTable table=new System.Data.DataTable ();
System.Data.DataColumn col=new System.Data.DataColumn ("Col1", typeof (String), expression);
table. Columns.Add (Col);
table. Rows.Add (new object[] {""});
return table. ROWS[0][0];
}
}