Is There a String Math Evaluator in .Net

Is there a string math evaluator in .NET?

You could add a reference to Microsoft Script Control Library (COM) and use code like this to evaluate an expression. (Also works for JScript.)

Dim sc As New MSScriptControl.ScriptControl()
sc.Language = "VBScript"
Dim expression As String = "1 + 2 * 7"
Dim result As Double = sc.Eval(expression)

Edit - C# version.

MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
string expression = "1 + 2 * 7";
object result = sc.Eval(expression);
MessageBox.Show(result.ToString());

Edit - The ScriptControl is a COM object. In the "Add reference" dialog of the project select the "COM" tab and scroll down to "Microsoft Script Control 1.0" and select ok.

Parse Math Expression

You can try using DataTable.Compute.

A related one is DataColumn.Expression.

Also check out: Doing math in vb.net like Eval in javascript

Note: I haven't used these myself.

Evaluating a mathematical expression

I went for Ncalc.
I am posting my codes for future users who will be on same problems like me.

1.Download the Ncalc(Binaries)http://ncalc.codeplex.com/releases/view/73656

  1. Reference the dll in your solution.(Right click > add reference > Browse > NCalc.dll)
  2. In codes

    Using NCalc;

3.May be used as

public Double Calculate(string argExpression)
{
//get the user passed string
string ExpressionToEvaluate = argExpression;
//pass string in the evaluation object declaration.
Expression z = new Expression(ExpressionToEvaluate);
//command to evaluate the value of the **************string expression
var result = z.Evaluate();
Double results = Convert.ToDouble(result.ToString());

return results;

}

Evaluate string with math operators

Use Ncalc:

Expression e = new Expression("(4+8)*2");
Debug.Assert(24 == e.Evaluate());

http://ncalc.codeplex.com/

Also, this question had been previously asked and has some interesting answers including Ncalc : Evaluating string "3*(4+2)" yield int 18

VB.Net- Evaluating Mathematical Expression in a String

You'll need a math expression parser to handle this.

Here are some various open source options on CodePlex:

  • Simple Math Parser
  • Fast Lightweight Expression Evaluator
  • ILCalc

A search will find many others...

how to evaluate ( math ) a string expression, vb.net

You can use the DataTable.Compute-"trick":

Dim tbl = new DataTable()
Dim result = Convert.ToDouble(tbl.Compute("4+4", Nothing))

The following arithmetic operators are supported in expressions:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)

More informations: DataColumn.Expression at Expression Syntax.

Convert Expression from a Textbox to Math Expression in Code Behind

solved with this library http://www.codeproject.com/Articles/21137/Inside-the-Mathematical-Expressions-Evaluator

my final code

Calculator Cal = new Calculator();
txt_LambdaNoot.Text = (Cal.Evaluate(txt_C.Text) / fo).ToString();

now when some one type 3*10^11 he will get 300000000000



Related Topics



Leave a reply



Submit