EvalEx is a handy expression evaluator for Apex (ported from Java EvalEx), that allows to evaluate simple mathematical and boolean expressions.
Key Features:
- Uses Decimal for calculation and result
- Single class implementation, very compact
- No dependencies to external libraries
- Precision and rounding mode can be set
- Supports variables
- Standard boolean and mathematical operators
- Standard basic mathematical and boolean functions
- Custom functions and operators can be added at runtime
- Functions can be defined with a variable number of arguments (see MIN and MAX functions)
Decimal result = null;
RT_Expression expression = new RT_Expression('1+1/3');
result = expression.eval():
expression.setPrecision(2);
result = expression.eval():
result = new RT_Expression('(3.4 + -4.1)/2').eval();
result = new RT_Expression('SQRT(a^2 + b^2').with('a', '2.4').with('b', '9.253').eval();
Decimal a = Decimal.valueOf('2.4');
Decimal b = Decimal.valueOf('9.235');
result = new RT_Expression('SQRT(a^2 + b^2').with('a', a).with('b', b).eval();
result = new RT_Expression('2.4/PI').setPrecision(16).setRoundingMode(RoundingMode.UP).eval();
result = new RT_Expression('random() > 0.5').eval();
result = new RT_Expression('not(x<7 || sqrt(max(x,9,3,min(4,3))) <= 3))').with('x', '22.9').eval();
result = new RT_Expression('log10(100)').eval();
Operator | Description |
---|---|
+ | Additive operator |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator (Modulo) |
^ | Power operator |
Operator | Description |
---|---|
= | Equals |
== | Equals |
!= | Not equals |
<> | Not equals |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
&& | Boolean and |
|| | Boolean or |
* Boolean operators result always in a Decimal value of 1 or 0 (zero). Any non-zero value is treated as a true
value. Boolean not
is implemented by a function.
Function* | Description |
---|---|
NOT(expression) | Boolean negation, 1 (means true) if the expression is not zero |
IF(condition,value_if_true,value_if_false) | Returns one value if the condition evaluates to true or the other if it evaluates to false |
RANDOM() | Produces a random number between 0 and 1 |
MIN(e1,e2, ...) | Returns the smallest of the given expressions |
MAX(e1,e2, ...) | Returns the biggest of the given expressions |
ABS(expression) | Returns the absolute (non-negative) value of the expression |
ROUND(expression,precision) | Rounds a value to a certain number of digits, uses the current rounding mode |
FLOOR(expression) | Rounds the value down to the nearest integer |
CEILING(expression) | Rounds the value up to the nearest integer |
LOG(expression) | Returns the natural logarithm (base e) of an expression |
LOG10(expression) | Returns the common logarithm (base 10) of an expression |
SQRT(expression) | Returns the square root of an expression |
SIN(expression) | Returns the trigonometric sine of an angle (in degrees) |
COS(expression) | Returns the trigonometric cosine of an angle (in degrees) |
TAN(expression) | Returns the trigonometric tangens of an angle (in degrees) |
ASIN(expression) | Returns the angle of asin (in degrees) |
ACOS(expression) | Returns the angle of acos (in degrees) |
ATAN(expression) | Returns the angle of atan (in degrees) |
SINH(expression) | Returns the hyperbolic sine of a value |
COSH(expression) | Returns the hyperbolic cosine of a value |
TANH(expression) | Returns the hyperbolic tangens of a value |
RAD(expression) | Converts an angle measured in degrees to an approximately equivalent angle measured in radians |
DEG(expression) | Converts an angle measured in radians to an approximately equivalent angle measured in degrees |
* Functions names are case insensitive.
Constant | Description |
---|---|
PI | The value of PI, exact to 100 digits |
TRUE | The value one |
FALSE | The value zero |
Custom operators can be added easily, simply create an instance of RT_Expression.Operator
and add it to the expression.
Parameters are the operator string, its precedence and if it is left associative. The operators eval()
method will be
called with the Decimal values of the operands.
All existing operators can also be overridden.
Adding custom functions is as easy as adding custom operators. Create an instance of RT_Expression.Function
and add it to
the expression. Parameters are the function name and the count of required parameters. The functions eval()
method
will be called with a list of the Decimal parameters. A -1
as the number of parameters denotes a variable number of
arguments.
All existing functions can also be overridden.