1 //method One uses the Compute method in the DataTable for example: 1*2-(4/1) +2*4=62 stringformulate =string. Format ("{0}*{1}-{2}/{3} +{1}*{2}",1,2,4,1); 3DataTable dt =NewDataTable (); 4Response.Write (Dt.compute (Formulate,""). ToString ()); 5 6Response.Write ("<br/>"); 7 8 //method Two uses the powerful eval function in JS to add COM references with if-else judgment: Microsoft sctipt Control 1.09Msscriptcontrol.scriptcontrol sc =NewMsscriptcontrol.scriptcontrolclass (); TenSc. Language ="JavaScript"; One stringFormulate1 =string. Format ("if ({0}==2) (({0}*{1})-{3}+ ({1}*{2})) +{4}/{0}); else (1+2*3)",2,3,4,5,6); A ObjectObjresult =SC. Eval (FORMULATE1); - if(Objresult! =NULL) - { theResponse.Write (Objresult.tostring ());//1+12+3 -}
/** /Publicobject evalexpress (string sexpression) { = Microsoft.JScript.Vsa.VsaEngine.CreateEngine (); return Microsoft.JScript.Eval.JScriptEvaluate (Sexpression, VE);
Original link http://blog.csdn.net/smartsmile2012/article/details/8214989
C # Alternative Custom formula calculation string converted to a calculation formula, and the results of the calculation "reprint"