- Notifications
You must be signed in to change notification settings - Fork96
NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It supports a wide range of mathematical and logical operations.
License
ncalc/ncalc
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. Itsupports a wide range of mathematical and logical operations. NCalc can parse any expression and evaluate the result,including static or dynamic parameters and custom functions. NCalc targets .NET 9, .NET 8, .NET Standard 2.0 and NET Framework4.8.
Need help or want to learn more?Check our docs.
For additional information on the technique we used to create this framework pleasereadthis article.
Important
If you need help,please open an issue and include the expressionto help us better understand the problem.Providing this information will aid in resolving the issue effectively.
If you want to evaluate simple expressions:
dotnet add package NCalcSync
Wantasync
support at your functions and parameters?
dotnet add package NCalcAsync
Dependency Injection? We got you covered:
dotnet add package NCalc.DependencyInjection
varexpression=newExpression("2 + 3 * 5");Debug.Assert(17==expression.Evaluate());
Evaluates .NET data types
Debug.Assert(123456==newExpression("123456").Evaluate());// integersDebug.Assert(newDateTime(2001,01,01)==newExpression("#01/01/2001#").Evaluate());// date and timesDebug.Assert(123.456==newExpression("123.456").Evaluate());// floating point numbersDebug.Assert(true==newExpression("true").Evaluate());// booleansDebug.Assert("azerty"==newExpression("'azerty'").Evaluate());// strings
Handles mathematical functional from System.Math
Debug.Assert(0==newExpression("Sin(0)").Evaluate());Debug.Assert(2==newExpression("Sqrt(4)").Evaluate());Debug.Assert(0==newExpression("Tan(0)").Evaluate());
Evaluates custom functions
varexpression=newExpression("SecretOperation(3, 6)");expression.Functions["SecretOperation"]=(args)=>{return(int)args[0].Evaluate()+(int)args[1].Evaluate();};Debug.Assert(9==expression.Evaluate());
Handles unicode characters
Debug.Assert("経済協力開発機構"==newExpression("'経済協力開発機構'").Evaluate());Debug.Assert("Hello"==newExpression(@"'\u0048\u0065\u006C\u006C\u006F'").Evaluate());Debug.Assert("だ"==newExpression(@"'\u3060'").Evaluate());Debug.Assert("\u0100"==newExpression(@"'\u0100'").Evaluate());
Define parameters, even dynamic or expressions
varexpression=newExpression("Round(Pow([Pi], 2) + Pow([Pi2], 2) + [X], 2)");expression.Parameters["Pi2"]=newExpression("Pi * [Pi]");expression.Parameters["X"]=10;expression.DynamicParameters["Pi"]= _=>{Console.WriteLine("I'm evaluating π!");return3.14;};Debug.Assert(117.07==expression.Evaluate());
JSON Serialization
At .NET 8+, NCalc have built-in support to polymorphic JSON serialization usingSystem.Text.Json.
conststringexpressionString="{waterLevel} > 4.0";varlogicalExpression=LogicalExpressionFactory.Create(expressionString,ExpressionOptions.NoCache);//Created a BinaryExpression object.varjsonExpression=JsonSerializer.Serialize(parsedExpression);vardeserializedLogicalExpression=JsonSerializer.Deserialize<LogicalExpression>(jsonExpression);//The object is still a BinaryExpression.varexpression=newExpression(deserializedLogicalExpression);expression.Parameters=newDictionary<string,object>{{"waterLevel",4.0}};varresult=expression.Evaluate();
Caching
NCalc automatically cache the parsing of strings using aConcurrentDictionary
.You can also use ourMemory Cache plugin.
Lambda Expressions
varexpression=newExpression("1 + 2");Func<int>function=expression.ToLambda<int>();Debug.Assert(function());//3
Fast and lightweight parser creation tools bySébastien Ros that NCalc uses at itsparser.
Extension functions for NCalc to handle many general functions,
including string functions, switch, if, in, typeOf, cast etc.
Developed by David, Dan and all atPanoramic Data.
JavaScript Interpreter for .NET bySébastien Ros, the author of NCalc library.
Runs on any modern .NET platform as it supports .NET Standard 2.0 and .NET 4.6.1 targets (and up).
A TypeScript/JavaScript port of NCalc.
NCalc 101 is a simple web application that allows you to try out the NCalc expression evaluator, developedbyPanoramic Data.
JJMasterData is a runtime form generator from database metadata. It uses NCalc to evaluate expressions used in fieldvisibility and other dynamic behaviors.
The project usesNerdbank.GitVersioning tool to manage versions.
Each library build can be traced back to the original git commit.Read more aboutversioning here.
If you want to talk with us, get support or just get the latest NCalcnews,come to our discord server.
About
NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It supports a wide range of mathematical and logical operations.