Is there a string math evaluator in .NET?

前端 未结 16 1408
长情又很酷
长情又很酷 2020-11-22 01:01

If I have a string with a valid math expression such as:

String s = \"1 + 2 * 7\";

Is there a built in library/function in .NET that will p

16条回答
  •  一向
    一向 (楼主)
    2020-11-22 01:42

    Recently I was using mXparser, which is a math parser library for .NET and JAVA. mXparser supports basic formulas as well as very fancy / complicated ones (including variables, functions, operators, iteration and recursion).

    https://mxparser.codeplex.com/

    https://mathparser.org/

    A few usage examples:

    Example 1:

    Expression e = new Expression("1+2*7 + (sin(10) - 2)/3");
    double v = e.calculate();
    

    Example 2:

    Argument x = new Argument("x = 5");
    Expression e = new Expression("2*x+3", x);
    double v = e.calculate();
    

    Example 3:

    Function f = new Function("f(x,y) = sin(x) / cos(y)");
    Expression e = new Expression("f(pi, 2*pi) - 2", f);
    double v = e.calculate();
    

    Found recently - in case you would like to try the syntax (and see the advanced use case) you can download the Scalar Calculator app that is powered by mXparser.

    Best regards

提交回复
热议问题