https://github.com/soomin-kevin-sung/dotnet-calculation-engine
CalculationEngine is a useful tool for calculating complex formulas.
https://github.com/soomin-kevin-sung/dotnet-calculation-engine
calculate csharp dotnet engine expression formula formula-parser utility visualbasic
Last synced: 5 months ago
JSON representation
CalculationEngine is a useful tool for calculating complex formulas.
- Host: GitHub
- URL: https://github.com/soomin-kevin-sung/dotnet-calculation-engine
- Owner: soomin-kevin-sung
- License: mit
- Created: 2024-02-22T04:51:38.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-08T17:10:15.000Z (about 2 years ago)
- Last Synced: 2025-10-26T23:38:04.867Z (7 months ago)
- Topics: calculate, csharp, dotnet, engine, expression, formula, formula-parser, utility, visualbasic
- Language: C#
- Homepage:
- Size: 169 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CaculationEngine
[](https://github.com/soomin-kevin-sung/dotnet-calculation-engine/actions/workflows/build_test_publish.yml)
[](https://www.nuget.org/packages/KevinComponent.CalculationEngine/)



[](https://www.codefactor.io/repository/github/soomin-kevin-sung/dotnet-calculation-engine)
[](LICENSE.md)

**CalculationEngine** is a useful tool for calculating complex formulas.
It's inspired by [Jace.NET](https://github.com/pieterderycke/Jace), and some code was taken from [Jace.NET](https://github.com/pieterderycke/Jace).
## Simple Code
* Here is a simple piece of code that demonstrates what a CalculationEngine is.
```csharp
// Define CalculationEngine
var engine = new CalculationEngine();
// Calculate Formulas
var answer = engine.Calculate("1 + 3");
var answer2 = engine.Calculate("3 + 5 * 9");
var answer3 = engine.Calculate("pow(3, 2) + (2 - 3)");
/****************** RESULT ******************/
// answer : 4
// answer2 : 48
// answer3 : 8
```
## Features
### The CalculationEngine can calculate formulas that contain strings and objects.
* Calculating String Constants And Variables
```csharp
var engine = new CalculationEngine();
var variables = new Dictionary() { { "code", "PRESENT" } };
var answer = engine.Calculate("if(CODE == \"PRESENT\", \"TRUE\", \"FALSE\")", variables);
/****************** RESULT ******************/
// answer : "TRUE"
```
### The CalculationEngine can access the object properties by [PropertyConnector](https://github.com/soomin-kevin-sung/dotnet-calculation-engine/blob/master/src/KevinComponent/KevinComponent/Execution/PropertyConnector.cs)
* Point Class for formula
```csharp
public class Point
{
public Point(string name, double x, double y)
{
Name = name;
X = x;
Y = y;
}
public string Name { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
```
* Implementing a PropertyConnector to access Point class properties
```csharp
public class PointPropertyConnector : PropertyConnector
{
public PointPropertyConnector(bool caseSensitive) : base(caseSensitive)
{
}
protected override object? OnGetPropertyValue(object target, string propertyName)
{
if (target is Point point)
{
if (propertyName.Equals(ConvertPropertyName(".Name")))
return point.Name;
else if (propertyName.Equals(ConvertPropertyName(".X")))
return point.X;
else if (propertyName.Equals(ConvertPropertyName(".Y")))
return point.Y;
}
return null;
}
}
```
* Calculate formulas with the Point class
```csharp
var engine = new CalculationEngine(new CalculationEngineOptions()
{
PropertyConnectorFactory = caseSensitive => new PointPropertyConnector(caseSensitive)
});
var variables = new Dictionary()
{
{ "p1", new Point("A", 0, 1) },
{ "p2", new Point("B", 1.25, -2.56) },
{ "Code", "A" }
};
var answer = engine.Calculate("p1.x + p2.y", variables);
var answer2 = engine.Calculate("if ( Code == p1.Name, \"True\", \"False\")", variables);
/****************** RESULT ******************/
// answer : -2.56
// answer2 : "True"
```