0

We are using the Math.js library for evaluating the string expressions in Angular. Now we need to implement the same in C# for the same expression. Is there any equivalent library to Math.Js in C# or can we call Math.js library in C#?

below is an example string expression which can be evaluated using math js

a = 2, b = 3, c = 3, d = 5, e = 7
ceil(a)*b+(ceil(c)+1)*(d+e)
Ramakrishna Reddy
  • 347
  • 1
  • 2
  • 18
  • There's a whole System.Math class in C#. If you can be more specific as to what you are trying to do then we can provide better advice. Are you trying to evaluate "1+2" as a string value to get 3? – Ryan Thomas Sep 18 '20 at 13:32
  • added an example, that expression can be evaluated using Math class in C# – Ramakrishna Reddy Sep 18 '20 at 13:57

1 Answers1

0

The equivalent of Math.Js in C# would be the Math class that is a part of the System.Runtime.Extension.dll assembly. That provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions.

Down below is a conversion to Math class based on the example in your question.

    decimal a = 2;
    decimal b = 3;
    decimal c = 3;
    decimal d = 5;
    decimal e = 7;
    decimal result = Math.Ceiling(a) * b + (Math.Ceiling(c) + 1) * (d + e);

Working example: https://dotnetfiddle.net/GOsGsi

ZarX
  • 1,096
  • 9
  • 17
  • ceil(a)*b+(ceil(c)+1)*(d+e) --> this is string expression math.js will evaluate that like math.eval("ceil(a)*b+(ceil(c)+1)*(d+e)", object with input values) – Ramakrishna Reddy Sep 18 '20 at 21:51
  • If it's a string you want to evaluate then this thread should hopefully give you the answer: https://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net – ZarX Sep 18 '20 at 21:55
  • that thread helps, however, the string expressions are framed according to math.js conventions but if I use some C# libraries then that might not be fully compatible with the string. that is why I have asked is it possible to call math.js library in C# – Ramakrishna Reddy Sep 18 '20 at 22:46