Skip to main content
.NETStandard2.0

A fast library used to calculate a text based formula input all contained in a convienent serilizable object. Add your own custom functions and variables. Calculate Boolean, String and Float outputs.

Published
Apr 18, 2023
3y ago
Total Downloads
21.6K
21,600

Install

dotnet add package SerializableFormula --version 2.0.0
ProductVersions
.NET Standard
netstandard2.0

README

Serializable Formula

Serializable Formula is a fast library used to calculate a text based formula input all contained in a convienent serializable object. Add your own custom functions and variables. Calculate Boolean, String and Float outputs.

Usage


Initialization

Start by creating a simple SerializableFormula object, setting the desired result type and the formula text.

	var newFormula = new SerializableFormula();
	newFormula.FormulaResultType = TResultType.Float;
	newformula.FormulaText = "Average(a, b, c, d, e)";

Defining Custom Variables

Now you have to define each part of the function. The SerializableFormula object above has no ability to process each function until you tell it what each part does.

Define your variables:

	var a = new FormulaVariable("a", 4);
	var b = new FormulaVariable("b", 2);
	var c = new FormulaVariable("c", 7);
	var d = new FormulaVariable("d", 10);
	var e = new FormulaVariable("e", 20);
	newFormula.AddVariable(a);
	newFormula.AddVariable(b);
	newFormula.AddVariable(c);
	newFormula.AddVariable(d);
	newFormula.AddVariable(e);

Defining Functions

Define the Average function:

	var average = new TAverageFunction();
	newFormula.AddFunction(average);

The Average function is a custom function that comes bundled with the SerializableFormula. More about custom functions later.

Verify Syntax

Use the VerifySyntax() function to return an object SyntaxCheckResult which contains:

  • ExpressionError : A string containing a human friendly error message
  • ExpressionErrorToken : A string with just the erroneous text token
  • ExpressionErrorPosition : A integer representing the position in the formula string where the error occurs

Calculation

Finally, we can obtain our result by calling the function that matches our output type:

  • var result = newFormula.CalculateText();
  • var result = newFormula.CalculateLogical();
  • var result = newFormula.CalculateArithmetic();

Custom Functions

In order to create a custom function all you have to do is create a new object that implements IFormulaFunction. Once implemented you can add your function to a SerializableFormula using the AddFunction() method.

Default Identifiers

These identifiers are always a part of SerializableFormula. They cannot be used to name Functions or Variables.

Boolean

  • or
  • xor
  • and
  • true
  • false
  • not
  • if
  • case

Math Operators

  • +
  • -
  • <
  • <=
  • >
  • >=
  • <>
  • =
  • /
  • *
  • ^
  • (
  • )
  • ,

Changes from Previous Version

v1.10.0v2.0.0
Removed Frameworks
.NETFramework4.6
Removed Dependencies
TFPExpressionParser.NET(.NETFramework4.6)
Essy.Math.Statistical(.NETFramework4.6)
TFPExpressionParser.NET(.NETStandard2.0)

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.