Calculate Formulas

  • Thread starter Thread starter Gálos Zsuzsa
  • Start date Start date
G

Gálos Zsuzsa

Hi all,
I need to calculate Formulas.

For example:
dim strFormula as string="25*(12-6)"

I ned a DotNet Function to calculate this Formula (25*(12-6)=150):

dim dblValue as double = 150

Does anyone know how can i do this?

thanks a lot

Zsuzsa
 
you could call out to a JScript.NET module that then calls the built-in
JScript function, eval().

example:

EvalUtil.jsc::
===================================
import System.Console;

package Ionic
{
class JsUtil
{
public static function Eval(expr : String) : String
{
System.Console.WriteLine("expression: {0}", expr);
var result : String = eval(expr);
System.Console.WriteLine("result: {0}", result);
return result;
}
}
}

===================================
Eval.cs::
===================================
public class Test {

public static void Main() {

string str1= "((10*80) / 100)";
Ionic.JsUtil.Eval( str1 );
string str2= "(240/80)";
Ionic.JsUtil.Eval(str2);

Ionic.JsUtil.Eval(str1 + " - " + str2);

}
}


I don't know if it is possible to call directly into the JScript.NET runtime
from {vb, C#, J#, etc} without building the wrapper in JScript.
 
Gálos Zsuzsa said:
Hi all,
I need to calculate Formulas.

For example:
dim strFormula as string="25*(12-6)"

I ned a DotNet Function to calculate this Formula (25*(12-6)=150):

dim dblValue as double = 150

Does anyone know how can i do this?

thanks a lot

Zsuzsa
If what you're trying to accomplish, is to allow a formula to be entered as
a string, then calculate the resulting value, you're going to have to parse
the string and break it up into relevant parts, then convert the parts (as
appropriate ) to the correct numeric data types. I'd start by reading up on
"String Manipulation" in the Visual Studio .NET help, then follow that with
a read of "Convert Class" (.NET Framework Library).
Those articles should get you started.
 
Back
Top