ShYCalculator 0.9.3

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

ShYCalculator

Build Status Nuget License Powered By: Antigravity

ShYCalculator is a high-performance, thread-safe, and extensible mathematical expression evaluator for .NET, based on the Shunting-Yard algorithm.

Features

  • ๐Ÿš€ High Performance: Optimized for low allocation (Zero-Allocation paths where possible).
  • ๐Ÿงต Thread Safe: Stateless execution model designed for high-concurrency environments (Web APIs, Services).
  • ๐Ÿงฉ Extensible: Add custom functions, operators, and constants.
  • ๐Ÿ—๏ธ Builder Pattern: Fluent API for easy configuration.
  • ๐Ÿ›ก๏ธ Safe: No eval() or dynamic compilation risks; strictly parsed.
  • ๐Ÿ” Compiled Mode: Parse once, execute many times for maximum performance.
  • ๐Ÿ“… Date & Time: Robust support for date parsing, culture-specific formats, and time adjustments.
  • ๐ŸŒณ Deep Nesting: Supports complex nested expressions (ifs, ternaries, functions).

Quick Start

Installation

dotnet add package ShYCalculator

Basic Usage (Stateless / Thread-Safe)

Recommended for Web APIs or shared services.

var calculator = new ShYCalculatorBuilder()
    .WithAllExtensions()
    .Build();

var context = new Dictionary<string, double> {
    { "x", 10 },
    { "y", 20 }
};

var result = calculator.Calculate("x * y + sqrt(x)", context);
Console.WriteLine($"Result: {result.Value.Nvalue}"); // Output: 203.16...

Advanced Usage (Custom Functions)

var calculator = new ShYCalculatorBuilder()
    .WithMathematics()
    .WithConstant("TaxRate", 0.2)
    .Build();

var result = calculator.Calculate("100 * (1 + TaxRate)");

Dependency Injection

// Program.cs
builder.Services.AddShYCalculator(options => {
    options.WithAllExtensions();
});

// MyService.cs
public class MyService(ShYCalculator calculator) {
    public double CalculatePrice(double basePrice) {
        var context = new Dictionary<string, double> { { "basePrice", basePrice } };
        return calculator.Calculate("basePrice * 1.2", context).Value.Nvalue.Value;
    }
}

Compiled Mode (High Performance)

For scenarios where the formula is constant but variables change, use Compile to avoid reparsing.

var compiled = ShYCalculator.Compile("score * multiplier");
if (compiled.Success) {
    var runner = compiled.Value;
    var result = runner.Calculate(new Dictionary<string, double> { { "score", 100 }, { "multiplier", 1.5 } });
}

AST & Expression Validation

Retrieve the parsed AST for analysis or validate syntax without execution.

// AST
var result = calculator.Calculate("1 + 2 * 3", includeAst: true);
var nodeType = result.Ast?.Type; // "binary"

// Validation
var validation = ShYCalculator.Compile("1 + (2"); 
if (!validation.Success) Console.WriteLine(validation.Errors[0].Message);

License

Distributed under the MIT License. See LICENSE for more information.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

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
0.9.3 78 3/1/2026
0.9.2.14 87 2/20/2026
0.9.2.13 83 2/20/2026
0.9.2.12 89 2/19/2026
0.9.2.11 96 2/19/2026
0.9.2.10 88 2/16/2026
0.9.2.9 85 2/16/2026
0.9.2.6 87 2/14/2026
0.9.2.5 87 2/14/2026
0.9.2.4 91 2/13/2026
0.9.2.3 88 2/13/2026
0.9.0 89 2/13/2026