Calkt is a Kotlin library that supports parsing and calculating various expressions. Parser is written in a way to have an ability to be extended.
The result of running example/Main.kt:
Enter math expression to calculate: 2 + 2 * 2
Parsed as: 2 plus (2 times 2)
Result: 6
Enter an expression with units to calculate: (1km - 0.5mi) + 2 yd to inches
Parsed as: (((1 Kilometers) minus (0.5 Miles)) plus (2 Yards)).convert(Inches)
Result: 7886.272 Inches
Releases are available from Maven Central and the GitHub Releases page.
dependencies {
implementation("com.itzephir.calkt:units:$version")
}The library consists of several different modules:
Module with all basic types for parsing and calculating. Here
you can find ParseContext and CalculateContext as well as
functions to launch parsers/calculators.
Module with implementation of basic math expressions that any
calculator can calculate. This is where you can find logic to
calculate numbers combined with basic supported binary operators (
+, -, *, /) and unary operators (+, -). You can implement
your own operator, like
in this example (% operator).
Module with implementation of math expressions with units. It
depends on math module heavily and does not do any calculations
on its own. There is a logic to support calculation and
conversions of units like 1 km + 2, 1km to inches, etc.
Module with all the examples you need to know. If something is missing, PRs are welcome.