Text to Formula is a lightweight and robust Java library for evaluating mathematical expressions provided as strings. It supports basic arithmetic operations (+
, -
, *
, /
), parentheses for grouping, and decimal numbers, making it ideal for applications requiring dynamic mathematical computations.
- Arithmetic Operations: Supports addition (
+
), subtraction (-
), multiplication (*
), and division (/
). - Parentheses: Handles nested expressions with proper precedence (e.g.,
((10 + 2) * (5 - 4)) / 2
). - Decimal Support: Evaluates expressions with decimal numbers (e.g.,
2.5 + 3.7
). - Robust Error Handling: Throws meaningful exceptions for invalid expressions, division by zero, and mismatched parentheses.
- Whitespace Tolerance: Ignores spaces in expressions (e.g.,
2 + 4
and2+4
are equivalent). - Lightweight: No external dependencies, ensuring easy integration.
Add the text-to-formula
library to your project using your preferred build tool. The library is available on Maven Central.
Add the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.ktauchathuranga</groupId>
<artifactId>text-to-formula</artifactId>
<version>0.0.1</version>
</dependency>
Add the following to your build.gradle
:
dependencies {
implementation 'io.github.ktauchathuranga:text-to-formula:0.0.1'
}
Add the following to your build.gradle.kts
:
dependencies {
implementation("io.github.ktauchathuranga:text-to-formula:0.0.1")
}
Add the following to your build.sbt
:
libraryDependencies += "io.github.ktauchathuranga" % "text-to-formula" % "0.0.1"
Add the following to your project.clj
:
:dependencies [[io.github.ktauchathuranga/text-to-formula "0.0.1"]]
The library provides a simple API through the FormulaEvaluator
class. Below is an example demonstrating how to evaluate mathematical expressions.
import io.github.ktauchathuranga.FormulaEvaluator;
import io.github.ktauchathuranga.exception.FormulaEvaluationException;
public class Main {
public static void main(String[] args) {
FormulaEvaluator evaluator = new FormulaEvaluator();
try {
// Simple expression
double result1 = evaluator.evaluate("2 + 4");
System.out.println("2 + 4 = " + result1); // Outputs: 2 + 4 = 6.0
// Complex expression with parentheses
double result2 = evaluator.evaluate("((10 + 2) * (5 - 4)) / 2");
System.out.println("((10 + 2) * (5 - 4)) / 2 = " + result2); // Outputs: ((10 + 2) * (5 - 4)) / 2 = 6.0
// Expression with decimals
double result3 = evaluator.evaluate("2.5 + 3.7");
System.out.println("2.5 + 3.7 = " + result3); // Outputs: 2.5 + 3.7 = 6.2
} catch (FormulaEvaluationException e) {
System.err.println("Error evaluating expression: " + e.getMessage());
}
}
}
- Class:
io.github.ktauchathuranga.FormulaEvaluator
- Method:
double evaluate(String expression)
- Takes a string representing a mathematical expression.
- Returns the computed result as a
double
. - Throws
FormulaEvaluationException
for invalid inputs.
- Exception:
io.github.ktauchathuranga.exception.FormulaEvaluationException
- Handles errors like invalid characters, division by zero, or mismatched parentheses.
- Java Version: Java 22 or later (the library is compiled with Java 22).
- No External Dependencies: The library is self-contained.
If you need a version compatible with an earlier Java version (e.g., Java 21 or 17), please open an issue on the GitHub repository.
The full API documentation is available in the Javadoc, included in the Maven Central release. You can also generate it locally by running:
mvn javadoc:javadoc
To build the library from source:
-
Clone the repository:
git clone https://github.com/ktauchathuranga/text-to-formula.git cd text-to-formula
-
Build with Maven:
mvn clean package
-
Run tests:
mvn test
The compiled JAR will be available in the target
directory.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix:
git checkout -b feature/your-feature-name
- Make your changes and commit them with clear messages.
- Push to your fork and submit a pull request.
If you encounter bugs or have feature requests, please open an issue on the GitHub Issues page. Provide:
- A clear description of the issue.
- Steps to reproduce (if applicable).
- Expected and actual behavior.
- Any relevant logs or screenshots.
This project is licensed under the MIT License. See the LICENSE file for details.
- Built with the Shunting Yard algorithm for robust expression parsing.
- Thanks to the open-source community for inspiration and feedback.
For questions or support, contact the maintainer:
- Ashen Chathuranga ([email protected])
- GitHub: ktauchathuranga