By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

Calculate Formulas

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <ga*****@chello.hu> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
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

Nov 22 '05 #2

P: n/a
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" <ga*****@chello.hu> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
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

Nov 22 '05 #3

P: n/a

"Gálos Zsuzsa" <ga*****@chello.hu> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter - [MVP - .NET Academic]
Nov 22 '05 #4

P: n/a

"Gálos Zsuzsa" <ga*****@chello.hu> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter - [MVP - .NET Academic]
Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.