You can evaluate any mathematical expressions using exp4j in android studio.
-
Download exp4j binary jar from the official site. Download
-
Import exp4j into android studio by copying the jar files in app/libs folder.
-
Add the following line in your module build.gradle file dependencies.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
}
package com.example.expressionevaluator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calculable calc = null;
try {
calc = new ExpressionBuilder("(5 * 4 * 3 * 2 * 1) / (2 * 1) * (3 * 2 * 1)").build();
double result = calc.calculate();
Log.d("result", result);
} catch (UnknownFunctionException e) {
e.printStackTrace();
} catch (UnparsableExpressionException e) {
e.printStackTrace();
}
}
}