Darokrithia
Darokrithia

Reputation: 160

Is there a way to evaluate formulas in a text box with C# in aspx

I am making a small game in ASPX with my friends. We are working on a method that will break a string into commands and variables, and then do the math, but the code is slightly difficult and we would like to see if there is a short cut.

If I have a Button and a Textbox, is there a built in method which I can use to do math with that text box in order to find the value (e.g. the computer turns ((5-4)*(2+1) +(3/2))*2 into 9.

Upvotes: 0

Views: 397

Answers (1)

jplindgren
jplindgren

Reputation: 358

You can use a library called NCalc to do it for you.

Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());

A similar question was asked Evaluating string "3*(4+2)" yield int 18

Upvotes: 2

Related Questions