I need to compare two double values with relation type as string. Eg:
String relation = "<=";
double aAA=5.9,bBB=6.999999;
In the above example need to compare aAA relation bBB using java
I need to compare two double values with relation type as string. Eg:
String relation = "<=";
double aAA=5.9,bBB=6.999999;
In the above example need to compare aAA relation bBB using java
You could create a Map
mapping the Strings to the actual operations, e.g. as BiPredicate
.
Map<String, BiPredicate<Double, Double>> relations = new HashMap<>();
relations.put("<=", (a, b) -> a <= b);
relations.put(">=", (a, b) -> a >= b);
relations.put("==", (a, b) -> a == b);
// ...
String relation = "<=";
double aAA=5.9,bBB=6.999999;
boolean result = relations.get(relation).test(aAA, bBB);