MathExpression.net 1.1.0.2006

dotnet add package MathExpression.net --version 1.1.0.2006
NuGet\Install-Package MathExpression.net -Version 1.1.0.2006
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.1.0.2006" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MathExpression.net --version 1.1.0.2006
#r "nuget: MathExpression.net, 1.1.0.2006"
#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.
// Install MathExpression.net as a Cake Addin
#addin nuget:?package=MathExpression.net&version=1.1.0.2006

// Install MathExpression.net as a Cake Tool
#tool nuget:?package=MathExpression.net&version=1.1.0.2006

MathExpression.net

A simple .NET 5 Math Expression Parser / Interpreter.

This library interprets mathematical expressions from text input.

Important Note on Localization

Before Version 1.1.xx

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

Since Version 1.1.xx

Localization added. See Chapter Localization

Basic functions

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

Installation

Get the current Version from NuGet or build it from this GitHub Source

MathExpression.net on nuget.org

Localization

Localization is supported since version 1.1.xx. The default localization is invariant. To set your preferred localization for floating point numbers and date strings, add a CultureInfo as the last parameter to the EvaluateExpression call.

Example


var deCulture = new CultureInfo("de-DE");
var result = ExpressionEvaluator.EvaluateExpression<DateTime>("01.01.2021", deCulture);
            

Basic Math

All Basic Math Operators are supported.

+ = Addition

- = Subtraction

\ = Dividing

* = Multiplication

^ = Exponentiation

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"
            

Conditions

You can define a conditional Expressions (IF->THEN->ELSE) for special purposes

Examples

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!");

Strings / Text

Text can be compared or merged.

Possible Opperands:

+ : Concat Text

= : Text Equals

Examples

String Compare

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

String Concat

var result = ExpressionEvaluator.EvaluateExpression("'MyString' + 'MyString'");
____________________________________
// result.text ==  "MyStringMyString";

Date

There are a number of date functions.

  • Date Substraction
  • Date Comparsion (=, >, >=, <, ⇐)

Also some Conversion- and Datepartextraction functions for results from date operations are available.

Partial Result

  • days(date1 - date2) → Gets the number of Days between to Dates
  • months(date1 - date2) → Gets the number of month between to Dates
  • years(date1 - date2) → Gets the number of Years between to Dates

Dateparts:

  • day(date1) → Gets the Day of the given Date
  • month(date1) → Gets the Month of the given Date
  • year(date1) → Gets the Year of the given Date

Examples

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 from string 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

You can check for dates and numbers if they are in the range of an given pair

Syntax:

[MyValue] BETWEEN [RangeStart_Value] AND [RangeEnd_Value]

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;

Variables

You can pass variables to the parser to work with them inside the expression.

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!"

Generic Type Result

Typesave Results

Instead of getting a Result object as the result of EvaluateExpression, you can use the generic type T and get the concrete result of the expression.

Supported Types:

  • int
  • string
  • boolean
  • double
  • DateTime
  • DateRange
  • decimal
Get Typed Result


int result = ExpressionEvaluator.EvaluateExpression<int>($"1 + 1 ");

____________________________________
// int result -> 2



int result = ExpressionEvaluator.EvaluateExpression<string>($"1 + 1 ");

____________________________________
// string result -> "2"

Rounding

To round results, you can use the Keeyword "round" followed by the number of decimals

Examples:

round0(1.2336) → 1

round1(1.2336) → 1.2

round2(1.2336) → 1.23

round3(1.2336) → 1.234



int result = ExpressionEvaluator.EvaluateExpression<double>($"round2(10 / 3)");

____________________________________
// double result -> 3.33

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. 
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,211 10/27/2021
1.1.0.2005 5,855 10/27/2021
1.0.2.5006 290 10/24/2021
1.0.2.5005 295 10/24/2021
1.0.1.2447 356 10/24/2021
1.0.1.2439 349 10/24/2021
1.0.1.1876 286 10/23/2021
1.0.1.1062 282 10/23/2021
1.0.1.504 292 10/24/2021
1.0.1.503 289 10/24/2021
1.0.1.70 338 10/24/2021
1.0.1.25 342 10/24/2021
1.0.0 309 10/23/2021