Skip to content

ncalc/ncalc

NCalc

GitHub Actions Workflow Status Coverage NuGet NuGet Downloads Discord

NCalc is a fast, lightweight expression evaluator for .NET. It parses and evaluates mathematical and logical expressions with literals, operators, parameters, built-in functions, and custom functions.

NCalc targets modern .NET, .NET Standard 2.0, and .NET Framework 4.8.

Documentation

Start with the NCalc docs for the full language reference, runtime options, plugins, and API reference.

Useful entry points:

Install

Use NCalc.Core for the expression engine:

dotnet add package NCalc.Core

Important

NCalcSync and NCalcAsync are obsolete redirect packages. Use NCalc.Core directly.

Add optional packages only when you need them:

dotnet add package NCalc.LambdaCompilation
dotnet add package NCalc.DependencyInjection

Quick Examples

Evaluate an expression

using NCalc;

var result = new Expression("2 + 3 * 5").Evaluate();
// 17

Use parameters

var expression = new Expression("Round(Pow([x], 2) + [offset], 2)");
expression.Parameters["x"] = 3.14;
expression.Parameters["offset"] = 10;

var result = expression.Evaluate();
// 19.86

Add custom functions

var expression = new Expression("SecretOperation(3, 6)");
expression.Functions["SecretOperation"] = args =>
    (int)args.Evaluate(0) + (int)args.Evaluate(1);

var result = expression.Evaluate();
// 9

Compile to a lambda

Install NCalc.LambdaCompilation first.

using NCalc.LambdaCompilation;

var expression = new Expression("1 + 2");
Func<int> function = expression.ToLambda<int>();

var result = function();
// 3

For data types, operators, built-in functions, async evaluation, caching, expression serialization, and advanced configuration, check the docs.

Help

Open an issue for bugs or usage problems. Include the expression, the expected result, the actual result, and your NCalc version.

Learn More

Related Projects

Project Description
FastExpressionCompiler Fast compiler for C# expression trees. NCalc uses it for lambda compilation.
Jint JavaScript interpreter for .NET by Sebastien Ros, the original author of NCalc.
JJMasterData Runtime form generator that uses NCalc for field visibility and other dynamic behavior.
NCalc101 Web application for trying NCalc expressions, developed by Panoramic Data.
NCalcJS TypeScript/JavaScript port of NCalc.
PanoramicData.NCalcExtensions Extension functions for NCalc, including string functions, switch, if, in, typeOf, and cast.
Parlot Fast and lightweight parser creation tools by Sebastien Ros. NCalc uses Parlot in its default parser.

Using NCalc? Create a PR and add your project here!

Versioning

NCalc uses Nerdbank.GitVersioning. Each library build can be traced back to the original git commit. Read more in the release documentation.

Star History

Star History Chart

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.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Contributors