Math expression eval C# (document)

Matheval is a simple, fast and highly flexible math expression eval library for C# .NET. This library allows to evaluate mathematical expression, logical expression, string expression and datetime expression.
In this page, we show some usage examples and lists all supported operators, constants and functions to give you an overview of using and implementation this C# eval library

1.Object initialization

Expression expression = new Expression("(a + b) / 2 ");

or using default constructor + SetFomular() method:

Expression expression = new Expression();
expression.SetFomular("(a + b) / 2 ");

2.Bind variable

Expression expression = new Expression("(a + b) / 2 ");
expression.Bind("a", 3).Bind("b",5);

3. Eval expression c#

Expression expression = new Expression("(a + b) / 2 ");
expression.Bind("a", 3);
expression.Bind("b",5);
Object value = expression.Eval(); // will return 4

Online example: https://dotnetfiddle.net/9TjftX

4. Multiple methods just in one line (method chaining)

Object value = new Expression("(a + b) / 2 ").Bind("a", 3).Bind("b", 5).Eval();

5. Using conditional statements

IF/ELSE condition:

Expression expression = new Expression("IF(time>8, (HOUR_SALARY*8) + (HOUR_SALARY*1.25*(time-8)), HOUR_SALARY*time)");
//bind variable
expression.Bind("HOUR_SALARY", 10);
expression.Bind("time", 9);
//eval
Decimal salary = expression.Eval<Decimal>(); // will return 92.5

Online example: https://dotnetfiddle.net/whmbIf

SWITCH/CASE condition:

Expression expr = new Expression("SWITCH(name,1,'apple',2,'mango',3,'banana','N/A')");
//Bind variable
expr.Bind("name", 2);
//eval
String value = expr.Eval<String>(); //return "mango"

Online example: https://dotnetfiddle.net/EJVRoO

6. Validate expression before evaluate

Expression expression = new Expression("SUM(1,2,3) + true");
//validate expression
List<String> errors = expression.GetError(); 
if(errors.Count == 0)
{
  //No error
}

Online example: https://dotnetfiddle.net/PqFHhM

7. Get variables that appear in the expression

Expression expression = new Expression("sin(pi/2+x)-cos(x) = a");
//validate expression
List<String> errors = expression.GetError();
if (errors.Count == 0)
{
 // get variables
 List<String> variables = expression.getVariables();
 foreach (String variable in variables)
 {
  Console.WriteLine(variable); // will print x, a
 }
}

Online example: https://dotnetfiddle.net/YWN929

8. Disable functions

Expression expression = new Expression();
expression.DisableFunction("SIN");
expression.DisableFunction("COS");

or

Expression expression = new Expression();
expression.DisableFunction(new string[] { "SIN", "COS" });

9. Work with number expression

Example 1: Scale and rounding mode

Use setScale() method to set number of digits to the right of the decimal point. Use setRoundingMode() method to set rounding mode. You can choose 1 of the following options:

  • MidpointRounding.AwayFromZero
  • MidpointRounding.ToEven
  • MidpointRounding.ToZero
Expression expression = new Expression("pi/2");
expression.SetScale(3);
expression.SetRoundingMode(MidpointRounding.ToEven);
Decimal value = expression.Eval<Decimal>();//return 1.571

Example 2: Simple expression

Expression expr = new Expression('(a+b+c)/2');
expr.Bind("a",2);
expr.Bind("b",3);
expr.Bind("c",16);
Object value = expr.Eval(); // return 10.5

Example 3: Complex expression

Expression expr = new Expression("tan(a)^3-((3*sin(a)-sin(3*a))/(3*cos(a)+cos(3*a)))");
expr.Bind("a", System.Math.PI / 4);
Object value = expr.Eval(); // return 0

Online example: https://dotnetfiddle.net/lYPKMv

Example 4: Min, max

Expression expr = new Expression("MIN(a,b,c)");
expr.Bind("a", 2);
expr.Bind("b", 3);
expr.Bind("c", 16);
int value = expr.Eval<int>(); // return 2

Online example: https://dotnetfiddle.net/XRCjss

Example 5: Min, max with collection variable

Expression expr = new Expression("MAX(a)");
expr.Bind("a", new List<Decimal> { 10m, 9.6m, 135, 724.2m, 6m, -4m });
Decimal value = expr.Eval<Decimal>(); // return 724.2

Online example: https://dotnetfiddle.net/PrRFyG

Example 6: Sum, average

Expression expr = new Expression("SUM(a,b,c)");
expr.Bind("a", 2d);
expr.Bind("b", 3d);
expr.Bind("c", 16d);
Double sum = expr.Eval<Double>(); // return 21
expr.SetFomular("AVERAGE(a,b,c)");
Double avg = expr.Eval<Double>(); // return 7

Online example: https://dotnetfiddle.net/SMb8KJ

Example 7: Sum, average using collection variable

Expression expr = new Expression("SUM(a)");
expr.Bind("a", new List<Decimal> { 2m, 3m, 16m });
Decimal sum = expr.Eval<Decimal>(); // return 21
expr.SetFomular("AVERAGE(a)");
Decimal avg = expr.Eval<Decimal>(); // return 7

Online example: https://dotnetfiddle.net/mmrP7y

Example 8: Round, Floor, Ceiling

Expression expr = new Expression("ROUND(2.149, 1)");
Object value = expr.Eval(); //return 2.1
Expression expr = new Expression("FLOOR(2.149)");
Object value = expr.Eval(); //return 2
//Rounds 3.7 down to nearest multiple of 2
Expression expr = new Expression("FLOOR(3.7,2)");
Object value = expr.Eval(); //return 2
Expression expr = new Expression("CEILING(2.149)");
Object value = expr.Eval(); //return 3
//Rounds 1.5 up to the nearest multiple of 0.1
Expression expr = new Expression("CEILING(1.5, 0.1)");
Object value = expr.Eval(); //return 1.5

Online example: https://dotnetfiddle.net/i2qzmO

Example 9: Trigonometry calculation

Expression expr = new Expression('SIN(PI())^2+COS(PI())^2');
Object value = expr.Eval(); //return 1
Expression expr = new Expression("sin(a) - cos(a) = SQRT(2) * sin(a - pi / 4)");
bool value = expr.Bind("a", Math.PI/5).Eval<bool>(); //return true
Expression expr = new Expression("tan(a)^3-((3*sin(a)-sin(3*a))/(3*cos(a)+cos(3*a)))");
Decimal value = expr.Bind("a", Math.PI/6).Eval<Decimal>(); //return 0

Online example: https://dotnetfiddle.net/e6xRaQ

Example 10: Unary operator

Expression expr = new Expression("3--2+--3-(-(++2))");
Object value = expr.Eval(); //return 10

Online example: https://dotnetfiddle.net/byrMSb

10. Work with string

Example 1: Left, right

Expression expr = new Expression("IF(VALUE(LEFT(date,4))<2020,'n/a','generation of ' & LEFT(date,4))");
expr.Bind("date", "2022-01-20");
String value = expr.Eval<String>();

Online example: https://dotnetfiddle.net/Gz6lkn

Example 2: Calculate tax

Expression taxExpr = new Expression("IF(LOWER(TAX_CODE)='vat',amount*10/100,IF(LOWER(TAX_CODE)='gst',amount*15/100,0))");
taxExpr.Bind("TAX_CODE","GST");
taxExpr.Bind("amount", 5005m);
Decimal value = taxExpr.Eval<Decimal>();

Online example: https://dotnetfiddle.net/9msdHd

Example 3: Concatenate strings

Expression expr = new Expression("CONCAT('The United States of ', 'America')");
String value = expr.Eval<String>();//The United States of America

Online example: https://dotnetfiddle.net/X4sQPo

11. Work with datetime

December 30, 1899 date system

We use 1899 date system, the first day that is supported is December 30, 1899. Any date you supply to this library will be converted into a serial number that represents the number of elapsed days starting from December 30, 1899. Look at the sample below:

Expression expr = new Expression("DATETIME(2025,01,02,12,11,10)");
Decimal serialDate = expr.Eval<Decimal>(); // return 45659.507755 (days elapsed from December 30, 1899)
DateTime datetime = expr.Eval<DateTime>(); // return 2025/01/02 12:11:10

Setting holidays and weekend for expression context

Expression expr = new Expression("NETWORKDAYS(B5,C5)");
expr.SetHolidays(
        new DateTime[] {
            new DateTime(2017,11,23),
            new DateTime(2017, 12, 25),
            new DateTime(2018, 1, 1)
        }
    );
expr.SetWeekends(new Int32[] { 1, 2, 3 }); //Sunday = 0, ..., Saturday = 6
expr.Bind("B5", new DateTime(2017, 1, 15));
expr.Bind("C5", new DateTime(2018, 1, 21));
Decimal value = expr.Eval<Decimal>(); //return 212

Example 1: Calculate networkday

Expression expr = new Expression("NETWORKDAYS(DATEVALUE('2021-03-30'),DATEVALUE('2022-05-16'))");
int value = expr.Eval<int>(); // return 295
Expression expr = new Expression("NETWORKDAYS(startdate,enddate)");
expr.Bind("startdate", new DateTime(2021, 08, 11));
expr.Bind("enddate", new DateTime(2021, 10, 4));
Object value = expr.Eval(); // return 39

Example 2: Calculates the number of days, months, or years between two dates.

Expression expr = new Expression("DATEDIF(DATEVALUE('2021-01-31'),DATEVALUE('2029-08-29'),'Y')");
Object value = expr.Eval(); // return 8
Expression expr = new Expression("DATEDIF(DATEVALUE('2021-01-31'),DATEVALUE('2029-08-29'),'M')");
Object value = expr.Eval(); // return 102
Expression expr = new Expression("DATEDIF(DATEVALUE('2021-01-31'),DATEVALUE('2029-08-29'),'D')");
Object value = expr.Eval(); // return 3132
Expression expr = new Expression("DATEDIF(DATEVALUE('2021-01-31'),DATEVALUE('2029-08-29'),'MD')");
Object value = expr.Eval(); // return 29

Example 3: Calculates the number of days, hours, mins between two datetime.

Expression expr = new Expression("DATETIME(2021,2,28,15,50,0)-DATETIME(2021,2,21,23,0,0)");
Decimal value = expr.Eval<Decimal>(); // return 6.70139 (days)
Expression expr = new Expression("(DATETIME(2021,2,28,15,50,0)-DATETIME(2021,2,21,23,0,0))*24");
Decimal value = expr.Eval<Decimal>(); // return 160.8 (hours)
Expression expr = new Expression("(DATETIME(2021,2,28,15,50,0)-DATETIME(2021,2,21,23,0,0))*24*60");
Decimal value = expr.Eval<Decimal>(); // return 9650 (mins)

Example 4: Calculates the number of days, hours, mins between two timespan.

Expression expr = new Expression("(endtime-TIME(17,0,0))*24*60");
expr.Bind("endtime", new TimeSpan(18, 20, 0));
Decimal value = expr.Eval<Decimal>(); //return 80 (mins)

Example5: Substract, add datetime

Expression expr = new Expression("DATETIME(2021,2,28,15,50,0)-DATETIME(2021,2,21,23,0,0)");
Object value = expr.Eval(); // return 6.701389 (days)
Expression expr = new Expression("DATE(2021,2,28)-DATE(2021,2,21)");
Object value = expr.Eval(); // return 7 (days)
Expression expr = new Expression("DATE(2021,9,20)+3");
DateTime value = expr.Eval<DateTime>(); // return 2021-09-23
Expression expr = new Expression("TIME(10,10,0)+TIME(10,10,0)");
TimeSpan value = expr.Eval<TimeSpan>(); // return 20:20:00

12. Supported operators, constants and functions in this expression evaluator library

Supported Operators

OperatorDescription
+Additive operator / Unary plus / Concatenate string / Datetime addition
&Concatenate string
Subtraction operator / Unary minus / Datetime subtraction
*Multiplication operator, can be omitted in front of an open bracket
/Division operator
%Remainder operator (Modulo)
^Power operator

Supported conditional statements

Conditional statementDescription
IF(logical_condition, value_if_true, value_if_false)Example:
IF(2>1,"Pass","Fail")
SWITCH(expression, val1,result1, [val2,result2], …, [default])Example:
SWITCH(3+2,5,"Apple",7,"Mango",3,"Good","N/A")

Supported logical and math functions

Function*Description
AND(logical1, [logical2], …)Determine if all conditions are TRUE
OR(logical1, [logical2], …)Determine if any conditions in a test are TRUE
NOT(logical)To confirm one value is not equal to another
XOR(logical1, [logical2], …)Exclusive OR function
SUM(number1, [number2],…)Return sum of numbers supplied
AVERAGE(number1, [number2],…)Return average of numbers supplied
MIN(number1, [number2],…)Return the smallest value from the numbers supplied
MAX(number1, [number2],…)Return the biggest value from the numbers supplied
MOD(number, divisor)Get remainder of two given numbers after division operator.
ROUND(number, num_digits)Returns the rounded approximation of given number using half-even rounding mode
( you can change to another rounding mode)
FLOOR(number, significance)Rounds a given number towards zero to the nearest multiple of a specified significance
CEILING(number, significance)Rounds a given number away from zero, to the nearest multiple of a given number
POWER(number, power)Returns the result of a number raised to a given power
RAND()Produces a random number between 0 and 1
SIN(number)Returns the trigonometric sine of the angle given in radians
SINH(number)Returns the hyperbolic sine of a number
ASIN(number)Returns the arc sine of an angle, in the range of -pi/2 through pi/2
COS(number)Returns the trigonometric cos of the angle given in radians
COSH(number)Returns the hyperbolic cos of a number
ACOS(number)Returns the arc cosine of an angle, in the range of 0.0 through pi
TAN(number)Returns the tangent of the angle given in radians
TANH(number)Returns the hyperbolic tangent of a number
ATAN(number)Returns the arc tangent of an angle given in radians
ATAN2(x_number, y_number)Returns the arctangent from x- and y-coordinates
COT(number)Returns the cotangent of an angle given in radians.
COTH(number)Returns the hyperbolic cotangent of a number
SQRT(number)Returns the correctly rounded positive square root of given number
LN(number)Returns the natural logarithm (base e) of given number
LOG10(number)Returns the logarithm (base 10) of given number
EXP(number)Returns e raised to the power of given number
ABS(number)Returns the absolute value of given number
FACT(number)Returns the factorial of a given number
SEC(number)Returns the secant of an angle given in radians
CSC(number)Returns the cosecant of an angle given in radians
PI()Return value of Pi
RADIANS(degrees)Convert degrees to radians
DEGREES(radians)Convert radians to degrees
INT(number)Returns the Integer value of given number

Supported Constants

ConstantDescription
eThe value of e
PIThe value of PI
TRUEThe boolean true value
FALSEThe boolean false value
NULLThe null value

Supported text functions

FunctionDescription
LEFT(text, num_chars)Extracts a given number of characters from the left side of a supplied text string
RIGHT(text, num_chars)Extracts a given number of characters from the right side of a supplied text string
MID(text, start_num, num_chars)Extracts a given number of characters from the middle of a supplied text string
REVERSE(text)Reverse a string
ISNUMBER(text)Check if a value is a number
LOWER(text)Converts all letters in the specified string to lowercase
UPPER(text)Converts all letters in the specified string to uppercase
PROPER(text)Capitalizes words given text string
TRIM(text)Removes extra spaces from text
LEN(text)Returns the length of a stringtext
TEXT(value, [format_text])Convert a numeric value into a text string. You can use the TEXT function to embed formatted numbers inside text
Example:
TEXT(123) -> 123
TEXT(DATEVALUE("2021-01-23"),"dd-MM-yyyy") -> 23-01-2021
TEXT(2.61,"hh:mm") -> 14:38
TEXT(2.61,"[hh]") -> 62
TEXT(2.61,"hh-mm-ss") -> 14-38-24
TEXT(DATEVALUE("2021-01-03")-DATEVALUE("2021-01-01"),"[h]") -> 48
TEXT(TIME(12,00,00)-TIME(10,30,10),"hh hours and mm minutes and ss seconds") -> "01 hours and 29 minutes and 50 seconds"
REPLACE(old_text, start_num, num_chars, new_text)Replaces characters specified by location in a given text string with another text string
SUBSTITUTE(text, old_text, new_text)Replaces a set of characters with another
FIND(find_text, within_text, [start_num])Returns the location of a substring in a string (case sensitive)
SEARCH(find_text, within_text, [start_num])Returns the location of a substring in a string (case insensitive)
CONCAT(text1, text2, text3,…)Combines the text from multiple strings
ISBLANK(text)Returns TRUE when a given string is null or empty, otherwise return FALSE
REPT(text, repeat_time)Repeats characters a given number of times
CHAR(char_code)Return character from ascii code
CODE(char)Returns a ascii code of a character
VALUE(text)Convert numbers stored as text to numbers

Supported date&time functions

Datetime functions is only available in pro version!

FunctionDescription
DATE(year,month,day)Constructs a Date from Integer representations of the yearmonth (1=Jan), and day
DATEVALUE(date_string)Constructs a Date from String representations of the yearmonthday
TIME(hour,min,second)Constructs a Time instance from Integer representations of the hourminute, and second
SECOND(time_instance)
SECOND(datetime_instance)
Returns the second component of a Time, Datetime
MINUTE(time_instance)
MINUTE(datetime_instance)
Returns the minute component of a Time, Datetime
HOUR(time_instance)
HOUR(datetime_instance)
Returns the hour component of a Time, Datetime
DAY(date_instance)
DAY(datetime_instance)
Returns the day component of a Date, Datetime
MONTH(date_instance)
MONTH(datetime_instance)
Returns the month component of a Date, Datetime
YEAR(date_instance)
YEAR(datetime_instance)
Returns the year component of a Date, Datetime
NOW()Returns the current Datetime based on a GMT calendar
TODAY()Returns the current date in the current user’s time zone.
EDATE(start_date, months)Add month to Date, Datime
WEEKDAY(date_instance)Returns the day of the week corresponding to a date. The day is given as an integer, ranging from 1 (Sunday) to 7 (Saturday)
WEEKNUM(date_instance)Returns the week number of a specific date. For example, the week containing January 1 is the first week of the year, and is numbered week 1
WORKDAY(start_date_instance, days)Returns a number that represents a date that is the indicated number of working days before or after a date (the starting date).
NETWORKDAYS(start_date_instance, end_date_instance)Returns the number of whole workdays between two dates
EOMONTH(start_date_instance, months)Calculates the last day of the month after adding a specified number of months to a date, datetime
DATEDIF(start_date, end_date, unit)Calculates the number of days, months, or years between two dates.
DAYS(end_date, start_date)Returns the number of days between two dates