2

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?

Chanchal Zoarder
  • 107
  • 1
  • 14
  • 2
    There are no built-in ways (though you can search for "C# evaluate math expression" - it may give you some ideas). If you don't need full-fledged math expression evaluation, then you can just use a simple `switch` (or `map/Dictionary` of `Func`) to do the job. – Eugene Podskal Feb 28 '16 at 09:13
  • Possible duplicate of [Evaluate C# string with math operators](http://stackoverflow.com/questions/5838918/evaluate-c-sharp-string-with-math-operators) – Eugene Podskal Feb 28 '16 at 09:14

1 Answers1

3

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

Community
  • 1
  • 1
Leroy Kegan
  • 1,156
  • 10
  • 9