NCalcSync 4.4.0-alpha

This is a prerelease version of NCalcSync.
There is a newer version of this package available.
See the version list below for details.
dotnet add package NCalcSync --version 4.4.0-alpha
                    
NuGet\Install-Package NCalcSync -Version 4.4.0-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NCalcSync" Version="4.4.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NCalcSync" Version="4.4.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="NCalcSync" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NCalcSync --version 4.4.0-alpha
                    
#r "nuget: NCalcSync, 4.4.0-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=NCalcSync&version=4.4.0-alpha&prerelease
                    
Install NCalcSync as a Cake Addin
#tool nuget:?package=NCalcSync&version=4.4.0-alpha&prerelease
                    
Install NCalcSync as a Cake Tool

NCalc

Appveyor Coverage Tests NuGet NuGet Discord

NCalc is a mathematical expression evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.

Docs

Need help or want to learn more? Check our docs.

Project Description

NCalc is a .NET library for evaluating mathematical expressions. It can handle various types of expressions, including those with static or dynamic parameters, as well as custom functions. It is supported by any target framework that accommodates .NET Standard 2.0.

For additional information on the technique we used to create this framework please read this article: https://www.codeproject.com/Articles/18880/State-of-the-Art-Expression-Evaluation.

[!IMPORTANT] If you need help, please open an issue and include the expression to help us better understand the problem. Providing this information will aid in resolving the issue effectively.

Getting Started

If you want to evaluate simple expressions:

dotnet add package NCalcSync 

Want async support at event handlers?

dotnet add package NCalcAsync 

Dependency Injection? We got you covered:

dotnet add package NCalc.DependencyInjection

Functionalities

Simple Expressions

var expression = new Expression("2 + 3 * 5");
Debug.Assert(17 == expression.Evaluate());

Evaluates .NET data types

Debug.Assert(123456 == new Expression("123456").Evaluate()); // integers
Debug.Assert(new DateTime(2001, 01, 01) == new Expression("#01/01/2001#").Evaluate()); // date and times
Debug.Assert(123.456 == new Expression("123.456").Evaluate()); // floating point numbers
Debug.Assert(true == new Expression("true").Evaluate()); // booleans
Debug.Assert("azerty" == new Expression("'azerty'").Evaluate()); // strings

Handles mathematical functional from System.Math

Debug.Assert(0 == new Expression("Sin(0)").Evaluate());
Debug.Assert(2 == new Expression("Sqrt(4)").Evaluate());
Debug.Assert(0 == new Expression("Tan(0)").Evaluate());

Evaluates custom functions

var expression = new Expression("SecretOperation(3, 6)");
expression.EvaluateFunction += delegate(string name, FunctionArgs args)
    {
        if (name == "SecretOperation")
            args.Result = (int)args.Parameters[0].Evaluate() + (int)args.Parameters[1].Evaluate();
    };

Debug.Assert(9 == expression.Evaluate());

Handles unicode characters

Debug.Assert("経済協力開発機構" == new Expression("'経済協力開発機構'").Evaluate());
Debug.Assert("Hello" == new Expression(@"'\u0048\u0065\u006C\u006C\u006F'").Evaluate());
Debug.Assert("だ" == new Expression(@"'\u3060'").Evaluate());
Debug.Assert("\u0100" == new Expression(@"'\u0100'").Evaluate());

Define parameters, even dynamic or expressions

var expression = new Expression("Round(Pow([Pi], 2) + Pow([Pi2], 2) + [X], 2)");

expression.Parameters["Pi2"] = new Expression("Pi * [Pi]");
expression.Parameters["X"] = 10;

expression.EvaluateParameter += delegate(string name, ParameterArgs args)
  {
    if (name == "Pi")
    args.Result = 3.14;
  };

Debug.Assert(117.07 == expression.Evaluate());

Caching and Serializing

This example uses Newtonsoft.Json.

Serializing

var parsedExpression = LogicalExpressionFactory.Create(expression, ExpressionOptions.NoCache);
var serialized = JsonConvert.SerializeObject(parsedExpression, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All // We need this to allow serializing abstract classes
});

Deserializing

var deserialized = JsonConvert.DeserializeObject<LogicalExpression>(serialized, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});

Expression.CacheEnabled = false; // We cannot use NCalc's built in cache at the same time.

var expression = new Expression(deserialized);
expression.Parameters = new Dictionary<string, object> {
    {"waterlevel", inputValue}
};

var result = expression.Evaluate();

You can also use our Memory Cache plugin.

Lambda Expressions

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

Parlot

Fast and lightweight parser creation tools by Sébastien Ros that NCalc uses at its parser.

PanoramicData.NCalcExtensions

Extension functions for NCalc to handle many general functions,
including string functions, switch, if, in, typeOf, cast etc.
Developed by David, Dan and all at Panoramic Data.

Jint

JavaScript Interpreter for .NET by Sé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).

NCalcJS

A TypeScript/JavaScript port of NCalc.

NCalc101

NCalc 101 is a simple web application that allows you to try out the NCalc expression evaluator, developed by Panoramic Data.

JJMasterData.NCalc

Plugin of NCalc used to evaluate JJMasterData expressions. JJMasterData is a runtime form generator from database metadata.

NCalc versioning

The project uses Nerdbank.GitVersioning tool to manage versions.
Each library build can be traced back to the original git commit.

Preparing and publishing a new release

  1. Make sure that nbgv dotnet CLI tool is installed and is up-to-date
  2. Run nbgv prepare-release to create a stable branch for the upcoming release, i.e. release/v1.0
  3. Switch to the release branch: git checkout release/v1.0
  4. Execute unit tests, update the README, release notes in csproj file, etc. Commit and push your changes.
  5. Run dotnet pack -c Release and check that it builds Nuget packages with the right version number.
  6. Run nbgv tag release/v1.0 to tag the last commit on the release branch with your current version number, i.e. v1.0.7.
  7. Push tags as suggested by nbgv tool: git push origin v1.0.7
  8. Go to GitHub project page and create a release out of the last tag v1.0.7.
  9. Verify that github workflow for publishing the nuget package has completed.
  10. Switch back to master and merge the release branch.

Discord Server

If you want to talk with us, get support or just get the latest NCalc news, come to our discord server.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on NCalcSync:

Package Downloads
JJMasterData.Core

JJMasterData library to render JJMasterData's components.

PGCG.commonItems

Package Description

NCalc.DependencyInjection

NCalc is a mathematical expression evaluator for .NET. This assembly brings Dependency Injection support for NCalc.

Asgard.Yggdrasil

阿斯加德框架集核心库

PikTools.NonModelElements.Shared

Набор инстументов, содержащих общую логику приложения "Создание немоделируемых элементов"

GitHub repositories (8)

Showing the top 8 popular GitHub repositories that depend on NCalcSync:

Repository Stars
squiggythings/WaveTracker
A free and open source music-making software for Windows. Uses wavetable synthesis and sampling to generate sounds.
textadventures/quest
Create text adventure games
MrCMS/MrCMS
Mr CMS is an open source C# MVC CMS Framework
AscensionGameDev/Intersect-Engine
Intersect provides a complete game development suite for creating 2d mmorpgs with no programming experience required!
egvijayanand/dotnet-maui-samples
.NET MAUI Samples
JJConsulting/JJMasterData
.NET CRUD generator library with Bootstrap support to create dynamic forms at runtime from a data dictionary.
naweed/MauiScientificCalculator
Scientific Calculator built using #dotnetmaui
Seddryck/NBi
NBi is a testing framework (add-on to NUnit) for Business Intelligence and Data Access. The main goal of this framework is to let users create tests with a declarative approach based on an Xml syntax. By the means of NBi, you don't need to develop C# or Java code to specify your tests! Either, you don't need Visual Studio or Eclipse to compile your test suite. Just create an Xml file and let the framework interpret it and play your tests. The framework is designed as an add-on of NUnit but with the possibility to port it easily to other testing frameworks.
Version Downloads Last updated
5.5.0-alpha 510 2/14/2025
5.4.1 22,016 2/14/2025
5.4.0 683 2/14/2025
5.3.1 33,070 12/18/2024
5.3.0 27,199 11/16/2024
5.2.11 79,437 10/25/2024
5.2.10 25,401 10/7/2024
5.2.9 15,099 9/26/2024
5.2.8 44,753 9/3/2024
5.2.7 1,311 9/2/2024
5.2.6 2,601 8/30/2024
5.2.5 1,528 8/29/2024
5.2.4 547 8/28/2024
5.2.3 5,434 8/23/2024
5.2.2 18,761 8/9/2024
5.2.1 8,103 8/6/2024
5.2.0 39,408 7/26/2024
5.1.0 9,051 7/12/2024
5.0.0 15,223 7/1/2024
4.4.0-alpha 207 6/10/2024
4.3.3 11,099 6/14/2024
4.3.2 2,764 6/12/2024
4.3.1 477 6/11/2024
4.3.0 452 6/10/2024
4.2.1 701 6/7/2024
4.2.0 1,783 6/4/2024
4.1.0 2,538 5/26/2024
4.0.0 9,332 5/21/2024
3.13.1 19,086 5/16/2024
3.13.0 4,186 5/6/2024
3.12.0 109,542 3/18/2024
3.11.0 99,240 2/26/2024
3.10.0 86,521 12/24/2023
3.9.0 26,293 12/5/2023
3.8.0 175,524 7/20/2023
3.7.0 15,254 7/14/2023
3.6.0 261,209 11/28/2022
3.5.0 121,058 6/10/2022
3.4.0 4,896 5/30/2022
3.2.0 1,085 5/26/2022
3.1.0 997 5/23/2022
3.0.0 1,210,831 12/22/2021
2.0.0 26,944 11/9/2020