MathExpression.net 1.0.1.2447

There is a newer version of this package available.
See the version list below for details.
dotnet add package MathExpression.net --version 1.0.1.2447
                    
NuGet\Install-Package MathExpression.net -Version 1.0.1.2447
                    
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="MathExpression.net" Version="1.0.1.2447" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MathExpression.net" Version="1.0.1.2447" />
                    
Directory.Packages.props
<PackageReference Include="MathExpression.net" />
                    
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 MathExpression.net --version 1.0.1.2447
                    
#r "nuget: MathExpression.net, 1.0.1.2447"
                    
#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.
#:package MathExpression.net@1.0.1.2447
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MathExpression.net&version=1.0.1.2447
                    
Install as a Cake Addin
#tool nuget:?package=MathExpression.net&version=1.0.1.2447
                    
Install as a Cake Tool

MathExpression.net

A simple .NET 5 Math Expression Parser / Interpreter.

This library interprets mathematical expressions from text input.

Important Note on Localization

Currently the Lib expects German localized input Strings (e.g. for Numbers: Use 2,5 instead 2.5 and Dates in Format dd.mm.yyy ) Will be corrected/extended in one of the next versions

Basic functions

  • Math Operations [+] [-] [\] [*] [^]
  • String, Date, Number Comparsion [<] [<=] [>] [>=] [=]
  • Date calculations (Range)
  • Extract Date parts (month, day, year)
  • Conditional statement IF THEN ELSE

Installation

MathExpression.net on nuget.org

Examples

Simple Math


var result = ExpressionEvaluator.EvaluateExpression("1+1");
____________________________________
// result.number -> 2;
// result.ToString() -> "2"
            

More Math


var result = ExpressionEvaluator.EvaluateExpression("((1 + 1) * 10 / (7 / 3.5)) ^ 2 / 10000");
____________________________________
// result.number -> 1;
// result.ToString() -> "1"
            

If Then Else

 var result = ExpressionEvaluator.EvaluateExpression($"IF 1 > 0 THEN 'Yes, it´s true!' ELSE 'No! Your wrong ..'");
____________________________________
// result.ToString() -> "Yes, it´s true!");

If Then Else (AND/OR)

 var result = ExpressionEvaluator.EvaluateExpression($"IF (1 > 0 AND 'Yes' != 'No') OR 100/10=10 THEN 'Yes, it´s true!' ELSE 'No! Your wrong ..'");
____________________________________
// result.ToString() -> "Yes, it´s true!");

String Comparsion


var result = ExpressionEvaluator.EvaluateExpression("'MyString' = 'MyString'");
____________________________________
// result.boolean ==  True;

Date Calculation

var d1 = new DateTime(2020, 01, 01);
var d2 = new DateTime(2019, 01, 01);
var result = ExpressionEvaluator.EvaluateExpression($"{d1} - {d2}");
____________________________________
// result.dateRange.TotalDays -> 365
var d1 = new DateTime(2020, 01, 01);
var d2 = new DateTime(2019, 01, 01);
var result = ExpressionEvaluator.EvaluateExpression($"days({d1} - {d2})");
____________________________________
// result.number -> 365;

Date with strings Calculation

var d1 = "01.01.2020";
var d2 = "01.01.2019";
var result = ExpressionEvaluator.EvaluateExpression($"{d1} - {d2}");
____________________________________
// result.dateRange.TotalDays -> 365

Date Parts

var d1 = new DateTime(2021, 01, 01);
var result = ExpressionEvaluator.EvaluateExpression($"Year({d1})");
____________________________________
// result.ToString() -> "2021";
            

Between Date

var d1 = "01.01.2020";
var d2 = "31.12.2020";
var d3 = "15.06.2020";
var result = ExpressionEvaluator.EvaluateExpression($"{d3} BETWEEN {d1} AND {d2}");
____________________________________
// result.boolean -> True;

Between Numbers

var d1 = 1;
var d2 = 10;
var d3 = 5;
var result = ExpressionEvaluator.EvaluateExpression($"{d3} BETWEEN {d1} AND {d2}");
____________________________________
// result.boolean -> True;

Using Variables

var values = new Dictionary<string, object>
            {
                { "FirstVar", 1.75 },
                { "SecondVar", 2 },
                { "ResultText", "Your Right!" }
            };

var result = ExpressionEvaluator.EvaluateExpression($"IF FirstVar < SecondVar THEN ResultText", values);
____________________________________
// result.ToString() -> "Your Right!"

Special Remarks

Special Shout-Out to Christian Parpart as initial Author - a long Time ago!

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0.2006 2,717 10/27/2021
1.1.0.2005 5,981 10/27/2021
1.0.2.5006 368 10/24/2021
1.0.2.5005 384 10/24/2021
1.0.1.2447 436 10/24/2021
1.0.1.2439 427 10/24/2021
1.0.1.1876 369 10/23/2021
1.0.1.1062 366 10/23/2021
1.0.1.504 369 10/24/2021
1.0.1.503 371 10/24/2021
1.0.1.70 421 10/24/2021
1.0.1.25 422 10/24/2021
1.0.0 392 10/23/2021