 # how to calculate any kind of formula? RSS

## 4 replies

Last post Aug 15, 2019 09:46 AM by bonsai8008

• ### how to calculate any kind of formula?

Hi

I have the following problem: I have any formula: e.g. (10+5)^3-255/5...

In asp classic I could calculate the formula by using: execute("result = " & Formula)

In asp.net I can't use 'execute' anymore. how can I calculate the formula? I had a look in the math-class but can't find there anything.

best regards

Chris

• ### Re: how to calculate any kind of formula?

Aug 12, 2019 07:06 AM|Yang Shen|LINK

Hi bonsai8008,

#### bonsai8008

In asp.net I can't use 'execute' anymore. how can I calculate the formula? I had a look in the math-class but can't find there anything.

As far as i know, in asp.net there's no such method to calculate a formula in a string format.

You may need to write your own calculate method. I believe you can find what you want here: MathParser.

Best Regard,

Yang Shen

• ### Re: how to calculate any kind of formula?

Hi Yang Shen

What a pity. Something quite interesting and surly often used is in asp.net not usable anymore. Will try to find out how to do it.

best regards

Chris

• ### Re: how to calculate any kind of formula?

IIRC, Server.Execute() invokes an asp page.  Eval() can invoke a string equation.

In ASP.NET is a different framework which uses objects but you can use code blocks to do something similar.

`Results = <%= 1+2+3 %>`

Or from the Code behind.

`Results = <%= Results %>`
```        public decimal Results { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Results = 1 + 2 + 3;
}```

• ### Re: how to calculate any kind of formula?

Thanks to all for the answers

I found out an easy way:

1. you have to activate in the project the reference "Microsoft Script Control 1.0". this has the effect that in the directory "/bin" you will get the file "Interop.MSScriptControl.dll"
2. then you can use the following function:

Formel = (5+3)*(8/2)+3*5
Resultat = BerechneFormel(Formel)

Public Function BerechneFormel(Formula As String) As Double
Dim SC As New MSScriptControl.ScriptControl
Dim Result As Double

Response.Write("<br>Z 1050) Formula: " + Formula)
SC.Language = "VBSCRIPT"

Formula = Replace(Formula, ".", "") 'mögliche Tausendertrennung
Formula = Replace(Formula, ",", ".") 'Umwandlung in amerikanische Zahlen

Try
'SHOW THAT IT WAS VALID
Result = Convert.ToDouble(SC.Eval(Formula))
'MessageBox.Show("Math success, " & Formula & " equals " & Result.ToString)

Catch ex As Exception
'SHOW THAT IT WAS INVALID
'MessageBox.Show("Not a valid math formula for a double.") '==> you need a box called "MessageBox"
End Try

BerechneFormel = Result
'Response.Write("<br>Z 1063) Result: " + Result.ToString)
End Function