-1

I have a Javascript formula saved in Database as a string and want to calculate it using JINT in C#.

The formula has multiple variables and need to set them with their values.

int height = 10
int length = 10
int width = 5

string jsFormula = "height * length * width";

I need to set all 3 variables using JINT engine and get the output.

4

1 回答 1

0

You may do this in several ways.

Create Engine object,

var formulaEngine = new Engine();

Set variables one by one,

formulaEngine.SetValue("height", height);
formulaEngine.SetValue("width", width);
formulaEngine.SetValue("length", length);

double result = formulaEngine.Execute(jsFormula ).GetCompletionValue().AsNumber();

OR

double result= new Engine()
            .SetValue("height", height) 
            .SetValue("width", width)
            .SetValue("length", length)
            .Execute("x * x") 
            .GetCompletionValue() 
            .AsNumber();
于 2019-04-04T02:46:42.290 回答