diff options
author | Adrien Hopkins <masterofnumbers17@gmail.com> | 2019-11-28 14:16:02 -0500 |
---|---|---|
committer | Adrien Hopkins <masterofnumbers17@gmail.com> | 2019-11-28 14:16:02 -0500 |
commit | e1e7e38482de8cf8f6d4fd8735b6e43ad2a13cb8 (patch) | |
tree | 11b2268eaa0a3fd56669b90516daca3e49de3b63 | |
parent | 908a9024f13a24def5d5e82ea08f7b5257a0426c (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)
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 29 | ||||
-rw-r--r-- | src/org/unitConverter/unit/UnitDatabase.java | 7 | ||||
-rw-r--r-- | unitsfile.txt | 2 |
3 files changed, 23 insertions, 15 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)); } /** diff --git a/src/org/unitConverter/unit/UnitDatabase.java b/src/org/unitConverter/unit/UnitDatabase.java index 9df34a7..65d52bf 100644 --- a/src/org/unitConverter/unit/UnitDatabase.java +++ b/src/org/unitConverter/unit/UnitDatabase.java @@ -978,9 +978,10 @@ public final class UnitDatabase { static { // place brackets around any expression of the form "number unit", with or without the space EXPRESSION_REPLACEMENTS.put(Pattern.compile("((?:-?[1-9]\\d*|0)" // integer - + "(?:\\.\\d+)?)" // optional decimal point with numbers after it + + "(?:\\.\\d+(?:[eE]\\d+))?)" // optional decimal point with numbers after it + "\\s*" // optional space(s) - + "([a-zA-Z]+)" // any string of letters + + "([a-zA-Z]+(?:\\^\\d+)?" // any string of letters + + "(?:\\s+[a-zA-Z]+(?:\\^\\d+)?))" // optional other letters + "(?!-?\\d)" // no number directly afterwards (avoids matching "1e3") ), "\\($1 $2\\)"); } @@ -1473,8 +1474,6 @@ public final class UnitDatabase { modifiedExpression = replacement.getKey().matcher(modifiedExpression).replaceAll(replacement.getValue()); } - System.out.println(modifiedExpression); - return this.prefixExpressionParser.parseExpression(modifiedExpression); } diff --git a/unitsfile.txt b/unitsfile.txt index bda9b81..a067d14 100644 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -121,7 +121,7 @@ henry V s / A H henry tesla Wb / m^2 T tesla -hertz s^-1 +hertz 1 / s Hz hertz gram millikg |