Chanchal Zoarder
Chanchal Zoarder

Reputation: 107

How can I parse math operator from string in c#

I've a dropdown like this

                    @Html.DropDownList("Operator", new List<SelectListItem>
                {
                    new SelectListItem() {Text = "/", Value = "/", Selected = true},
                    new SelectListItem() {Text = "*", Value = "*"},
                    new SelectListItem() {Text = "+", Value = "+"},
                    new SelectListItem() {Text = "-", Value = "-"}
                }, new {@Class = "form-control", @Id = "Operator"})

In my action method I want to use var1 Operator var2. Suppose 10 Operator 15 = 25 when Operator = "+" Is it possible?

Upvotes: 2

Views: 2157

Answers (1)

Leroy Kegan
Leroy Kegan

Reputation: 1216

Recently I was using open-source library for parsing math expressions provided as strings. The parser / evaluator name is mXparser.

http://mxparser.codeplex.com/

http://mathparser.org/

Example:

Expression e = new Expression("2+3/(4+1)");
double v = e.calculate();

Additionally - this software is using mXparser as well - you can learn the syntax Scalar Calculator app.

Best regards

Upvotes: 3

Related Questions