summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/converterGUI
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-11-28 14:16:02 -0500
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-11-28 14:16:02 -0500
commite1e7e38482de8cf8f6d4fd8735b6e43ad2a13cb8 (patch)
tree11b2268eaa0a3fd56669b90516daca3e49de3b63 /src/org/unitConverter/converterGUI
parent908a9024f13a24def5d5e82ea08f7b5257a0426c (diff)
You can now perform reciprocal conversions (e.g. s <-> Hz)
Fixed some bugs in unit-number ordering ("10 m^2" was interpreted as "(10 m)^2", e-notation was ignored)
Diffstat (limited to 'src/org/unitConverter/converterGUI')
-rw-r--r--src/org/unitConverter/converterGUI/UnitConverterGUI.java29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
index 0be6c9b..b84e730 100644
--- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java
+++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
@@ -247,20 +247,29 @@ final class UnitConverterGUI {
return;
}
- // if I can't convert, leave
- if (!from.canConvertTo(to)) {
+ if (from.canConvertTo(to)) {
+ value = from.convertTo(to, 1);
+
+ // round value
+ final String output = this.getRoundedString(value);
+
+ this.view.setExpressionConverterOutputText(
+ String.format("%s = %s %s", fromUnitString, output, toUnitString));
+ } else if (from instanceof LinearUnit && SI.ONE.dividedBy((LinearUnit) from).canConvertTo(to)) {
+ // reciprocal conversion (like seconds to hertz)
+ value = SI.ONE.dividedBy((LinearUnit) from).convertTo(to, 1);
+
+ // round value
+ final String output = this.getRoundedString(value);
+
+ this.view.setExpressionConverterOutputText(
+ String.format("1 / %s = %s %s", fromUnitString, output, toUnitString));
+ } else {
+ // if I can't convert, leave
this.view.showErrorDialog("Conversion Error",
String.format("Cannot convert between %s and %s", fromUnitString, toUnitString));
- return;
}
- value = from.convertTo(to, 1);
-
- // round value
- final String output = this.getRoundedString(value);
-
- this.view.setExpressionConverterOutputText(
- String.format("%s = %s %s", fromUnitString, output, toUnitString));
}
/**