formula_parser 2.0.1

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A parsing library to parse formulas and evaluate like we use in excel or spreadsheets.

Formula Parser for Dart

A dart plugin to to parse and evaluate mathematical expressions.

Pub Package GitHub Issues GitHub Forks GitHub Stars GitHub License

Formula Parser is a Dart library that allows you to parse and evaluate mathematical expressions. The grammar for this library is built using petitparser library.

This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there.

The package is hosted on dart packages. Up-to-date API documentation is created with every release.

Installation

To use Formula Parser in your Dart project, add the following dependency to your pubspec.yaml file:

dependencies:
  formula_parser: ^latest

Then, run flutter packages get

or to install it on another dart project, run the below command

dart pub add formula_parser

Usage

Import the package in your Dart file:

import 'package:formulaparser/formulaparser.dart';

Create a FormulaParser instance with the expression and variables:

var exp = FormulaParser('a+b+150', {'a': 10, 'b': 20 });

To parse the expression,

var result = exp.parse;

Check if the parsing was successful:

if (result.isSuccess) {
  print('Result: \${result.value}');
} else {
  print(result.isFailure);
  print(result.position);
  print(result.message);
}

Features

  • Parses and evaluates mathematical expressions.
  • Supports variables and functions in the expressions.
  • Handles error cases and provides error messages.

Supported Math Functions

The following math functions are supported by the Formula Parser:

FunctionDescriptionExample
ADDAdditionADD(2, 3)
SUBSubtractionSUB(5, 2)
MULMultiplicationMUL(4, 5)
DIVIDivisionDIVI(10, 2)
AVGAverageAVG(2, 3, 4, 5)
POWERExponentiationPOWER(2, 3)
SQRTSquare RootSQRT(4)
CEILCeilingCEIL(4.2)
FLOORFloorFLOOR(4.8)
ROUNDRoundROUND(4.5)
ABSAbsoluteABS(-4.5)
EXPExponentialEXP(2)
LOGNatural LogarithmLOG(10)
SINSineSIN(0)
ASINArcsineASIN(0)
COSCosineCOS(0)
ACOSArccosineACOS(0)
TANTangentTAN(0)
ATANArctangentATAN(0)

To use these functions, you can include them in your expressions. For example:

var exp = FormulaParser('1+ADD(4,4)+MUL(2,DIVI(2,3))+AVG(2,3,4,5)/5+POWER(2,2)+SQRT(4)+CEIL(4.2)+FLOOR(4.8)+ROUND(4.5)+ABS(-4.5)+EXP(2)+LOG(10)+SIN(0)+ASIN(0)+COS(0)+ACOS(0)+TAN(0)+ATAN(0)');

The following comparison functions are supported by the Formula Parser:

FunctionDescriptionExample
EQEqualEQ(2,2)
NENot EqualNE(2,3)
LTLess ThanLT(2,3)
GTGreater ThanGT(2,3)
GTELess Than or Equal ToLTE(2,3)
LTEGreater Than or Equal ToGTE(2,3)

Supported Operators

The following operators are supported by the Formula Parser:

OperatorDescriptionExample
+Addition2 + 3
-Subtraction5 - 2
*Multiplication4 * 5
/Division10 / 2
^Exponentiation2 ^ 3
Comparison Operators
==Equal2 == 2
!=Not Equal5 != 2
<Less Than4 < 5
>Greater Than10 > 2
<=Less Than or Equal To2 <= 3
>=Greater Than or Equal To5 >= 2

Documentation

The FormulaParser class provides the following methods and properties:

FormulaParser(expression: String, [options: Map<String, dynamic>])

Constructs a FormulaParser instance with the given expression and options.

  • expression: The mathematical expression to parse.
  • options (optional): A map of variables to their values.

error

Indicates whether an error occurred during parsing.

  • Returns true if an error occurred, false otherwise.

errorMessage

The error message describing the error that occurred during parsing.

  • Returns the error message as a String.

parsedExpression

The parsed expression after preprocessing.

  • Returns the parsed expression as a String.

parse

Parses the expression and returns the result.

  • Returns the parsed result.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.