Nglib.Formula 1.1.8-beta

This is a prerelease version of Nglib.Formula.
dotnet add package Nglib.Formula --version 1.1.8-beta
                    
NuGet\Install-Package Nglib.Formula -Version 1.1.8-beta
                    
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="Nglib.Formula" Version="1.1.8-beta" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nglib.Formula" Version="1.1.8-beta" />
                    
Directory.Packages.props
<PackageReference Include="Nglib.Formula" />
                    
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 Nglib.Formula --version 1.1.8-beta
                    
#r "nuget: Nglib.Formula, 1.1.8-beta"
                    
#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 Nglib.Formula@1.1.8-beta
                    
#: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=Nglib.Formula&version=1.1.8-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Nglib.Formula&version=1.1.8-beta&prerelease
                    
Install as a Cake Tool

Nglib.Formula

Formula parser and evaluator - Expression engine for dynamic string calculations

Nglib.Formula is a powerful expression parser and evaluator that allows you to parse and execute dynamic formulas with parameters, functions, and operators.

Read Documentation

Features

  • Expression Parsing: Parse complex mathematical and logical expressions
  • Parameter Support: Dynamic parameter injection with @parameter syntax
  • Built-in Functions: Math, string, date, and logical functions
  • Custom Functions: Register your own custom functions
  • Operators: Arithmetic (+, -, *, /), comparison (==, !=, <, >, ⇐, >=), logical (&&, ||)
  • Type Safety: Automatic type conversion and validation
  • Performance: Optimized parsing with segment caching
  • Extensible: Easy to add new functions and operators

Installation

dotnet add package Nglib.Formula

Note: Requires Nglib package as dependency.

Quick Start

Simple Evaluation

using Nglib.FORMAT.FORMULA;

// Simple calculation
var result = FormulaTools.Evaluate("2 + 3 * 4"); // Returns 14

// With parameters
var context = new FormulaContext();
context.SetParameter("price", 100);
context.SetParameter("tax", 0.20);

var total = FormulaTools.Evaluate("@price * (1 + @tax)", context); // Returns 120

Using Functions

// Built-in functions
var result = FormulaTools.Evaluate("add(10, 20, 30)"); // Returns 60
var result = FormulaTools.Evaluate("mul(5, 3)"); // Returns 15
var result = FormulaTools.Evaluate("max(10, 25, 15)"); // Returns 25

// String functions
var result = FormulaTools.Evaluate("concat('Hello', ' ', 'World')"); // Returns "Hello World"
var result = FormulaTools.Evaluate("upper('hello')"); // Returns "HELLO"

// Conditional
var result = FormulaTools.Evaluate("if(10 > 5, 'yes', 'no')"); // Returns "yes"

Complex Formulas

var context = new FormulaContext();
context.SetParameter("quantity", 5);
context.SetParameter("unitPrice", 25.50);
context.SetParameter("discountRate", 0.10);

var formula = "mul(@quantity, @unitPrice) * (1 - @discountRate)";
var total = FormulaTools.Evaluate(formula, context); // Returns 114.75

Custom Functions

FormulaTools.RegisterFunction("double", args => {
    var value = Convert.ToDouble(args[0]);
    return value * 2;
});

var result = FormulaTools.Evaluate("double(21)"); // Returns 42

Segment Parsing (Advanced)

using Nglib.FORMAT.FORMULA.SEGMENT;

// Parse once, execute multiple times
var segments = FormulaSegmentParseTools.Parse("@price * (1 + @tax)");

// Execute with different contexts
var context1 = new FormulaContext();
context1.SetParameter("price", 100);
context1.SetParameter("tax", 0.20);
var result1 = FormulaTools.EvaluateSegments(segments, context1);

var context2 = new FormulaContext();
context2.SetParameter("price", 200);
context2.SetParameter("tax", 0.15);
var result2 = FormulaTools.EvaluateSegments(segments, context2);

Built-in Functions

Math

  • add(x, y, ...) - Addition
  • sub(x, y) - Subtraction
  • mul(x, y, ...) - Multiplication
  • div(x, y) - Division
  • mod(x, y) - Modulo
  • abs(x) - Absolute value
  • max(x, y, ...) - Maximum
  • min(x, y, ...) - Minimum
  • round(x, decimals) - Round

String

  • concat(str1, str2, ...) - Concatenate strings
  • upper(str) - Uppercase
  • lower(str) - Lowercase
  • substring(str, start, length) - Extract substring
  • length(str) - String length

Logical

  • if(condition, trueValue, falseValue) - Conditional
  • isnull(value, default) - Null check
  • equals(x, y) - Equality check

Date

  • now() - Current date/time
  • dateadd(date, days) - Add days to date
  • datediff(date1, date2) - Difference in days

📚 Documentation

Full documentation: https://github.com/NueGy/NgLib
Formula documentation https://github.com/NueGy/NgLib/docs/wiki_components_formula.md

Product Compatible and additional computed target framework versions.
.NET 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 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.
  • net6.0

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.8-beta 54 5/22/2026
1.1.7-beta 146 11/23/2025