zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

compute int as double #77

Closed cuiliang closed 4 years ago

cuiliang commented 4 years ago

Is there any option that can make expression "5 / 3" result to 1.6666667? I want to evaluate user-inputed expression to let them compute some numbers, they don't know what is integer what is double.

JonathanMagnan commented 4 years ago

Hello @cuiliang ,

Sure, that's possible, you are currently looking for the DefaultNumberType option.

EvalManager.DefaultContext.DefaultNumberType = DefaultNumberType.Double;

var result = Eval.Execute("5/3");
Console.WriteLine(result); // 1.66666666666667

https://dotnetfiddle.net/JgQGqJ

Obviously, it could have a side impact as a method expecting an int is no longer compatible if there is no double overload.

Let me know if that answers your question correctly.

Best Regards,

Jon

cuiliang commented 4 years ago

Yes, thank you. did not see this in document, perhaps it should be updated. https://eval-expression.net/options