diff options
Diffstat (limited to 'src/org/unitConverter/converterGUI')
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 29 |
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)); } /** |