diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2020-07-16 16:49:00 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2020-07-16 16:49:00 -0500 |
commit | 7c3c6c21ce20eb48cc5c1caa150f3bb950c6a93b (patch) | |
tree | 353ee64f090ae7238befb1a8d40e8ef64c30315b | |
parent | 573a3d9ba3392697233f75ea3a9c02839e07fde0 (diff) |
Added basic rounding settings.
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 88 |
1 files changed, 71 insertions, 17 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index aa81a74..daae2c6 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -24,6 +24,7 @@ import java.awt.event.KeyEvent; import java.io.File; import java.math.BigDecimal; import java.math.MathContext; +import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.FieldPosition; import java.text.NumberFormat; @@ -75,7 +76,18 @@ final class UnitConverterGUI { * A tab in the View. */ private enum Pane { - UNIT_CONVERTER, EXPRESSION_CONVERTER, UNIT_VIEWER, PREFIX_VIEWER; + UNIT_CONVERTER, EXPRESSION_CONVERTER, UNIT_VIEWER, PREFIX_VIEWER, SETTINGS; + } + + /** + * Different types of rounding. + * + * Significant digits: Rounds to a number of digits. i.e. with precision 5, 12345.6789 rounds to 12346. + * Decimal places: Rounds to a number of digits after the decimal point, i.e. with precision 5, 12345.6789 rounds to 12345.67890. + * Scientific: Rounds based on the number of digits and operations, following standard scientific rounding. + */ + private static enum RoundingType { + SIGNIFICANT_DIGITS, DECIMAL_PLACES, SCIENTIFIC; } private static class Presenter { @@ -125,7 +137,14 @@ final class UnitConverterGUI { private final Comparator<String> prefixNameComparator; - private int significantFigures = 6; + /* + * Rounding-related settings. I am using my own system, and not MathContext, + * because MathContext does not support decimal place based or scientific + * rounding, only significant digit based rounding. + */ + private int precision = 6; + + private RoundingType roundingType = RoundingType.SIGNIFICANT_DIGITS; /** * Creates the presenter. @@ -213,10 +232,10 @@ final class UnitConverterGUI { } final double value = from.convertTo(to, beforeValue); - final String output = this.getRoundedString(value); + final String output = this.getRoundedString(new BigDecimal(value)); - this.view.setDimensionConverterOutputText( - String.format("%s %s = %s %s", this.view.getDimensionConverterText(), fromSelection, output, toSelection)); + this.view.setDimensionConverterOutputText(String.format("%s %s = %s %s", + this.view.getDimensionConverterText(), fromSelection, output, toSelection)); } /** @@ -271,7 +290,7 @@ final class UnitConverterGUI { value = from.convertTo(to, 1); // round value - final String output = this.getRoundedString(value); + final String output = this.getRoundedString(new BigDecimal(value)); this.view.setExpressionConverterOutputText( String.format("%s = %s %s", fromUnitString, output, toUnitString)); @@ -280,7 +299,7 @@ final class UnitConverterGUI { value = SI.ONE.dividedBy((LinearUnit) from).convertTo(to, 1); // round value - final String output = this.getRoundedString(value); + final String output = this.getRoundedString(new BigDecimal(value)); this.view.setExpressionConverterOutputText( String.format("1 / %s = %s %s", fromUnitString, output, toUnitString)); @@ -316,10 +335,23 @@ final class UnitConverterGUI { * @since 2019-04-14 * @since v0.2.0 */ - private final String getRoundedString(final double value) { - // round value - final BigDecimal bigValue = new BigDecimal(value).round(new MathContext(this.significantFigures)); - String output = bigValue.toString(); + private final String getRoundedString(final BigDecimal value) { + // round value based on rounding type + final BigDecimal roundedValue; + switch (roundingType) { + case DECIMAL_PLACES: + roundedValue = value.setScale(precision, RoundingMode.HALF_EVEN); + break; + case SCIENTIFIC: + throw new UnsupportedOperationException("Not yet implemented."); + case SIGNIFICANT_DIGITS: + roundedValue = value.round(new MathContext(this.precision)); + break; + default: + throw new AssertionError("Invalid switch condition."); + } + + String output = roundedValue.toString(); // remove trailing zeroes if (output.contains(".")) { @@ -364,12 +396,12 @@ final class UnitConverterGUI { } /** - * @param significantFigures new value of significantFigures + * @param precision new value of precision * @since 2019-01-15 * @since v0.1.0 */ - public final void setSignificantFigures(final int significantFigures) { - this.significantFigures = significantFigures; + public final void setPrecision(final int precision) { + this.precision = precision; } /** @@ -416,6 +448,14 @@ final class UnitConverterGUI { public final Set<String> unitNameSet() { return this.database.unitMapPrefixless().keySet(); } + + /** + * @param roundingType the roundingType to set + * @since 2020-07-16 + */ + public final void setRoundingType(RoundingType roundingType) { + this.roundingType = roundingType; + } } private static class View { @@ -502,6 +542,8 @@ final class UnitConverterGUI { return Pane.UNIT_VIEWER; case 3: return Pane.PREFIX_VIEWER; + case 4: + return Pane.SETTINGS; default: throw new AssertionError("No selected pane, or invalid pane."); } @@ -824,14 +866,18 @@ final class UnitConverterGUI { final JRadioButton fixedPrecision = new JRadioButton("Fixed Precision"); fixedPrecision.setSelected(true); + fixedPrecision.addActionListener(e -> this.presenter.setRoundingType(RoundingType.SIGNIFICANT_DIGITS)); roundingRuleButtons.add(fixedPrecision); roundingPanel.add(fixedPrecision, new GridBagBuilder(0, 1).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton fixedDecimals = new JRadioButton("Fixed Decimal Places"); + fixedDecimals.addActionListener(e -> this.presenter.setRoundingType(RoundingType.DECIMAL_PLACES)); roundingRuleButtons.add(fixedDecimals); roundingPanel.add(fixedDecimals, new GridBagBuilder(0, 2).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton relativePrecision = new JRadioButton("Scientific Precision"); + relativePrecision.setEnabled(false); + relativePrecision.addActionListener(e -> this.presenter.setRoundingType(RoundingType.SCIENTIFIC)); roundingRuleButtons.add(relativePrecision); roundingPanel.add(relativePrecision, new GridBagBuilder(0, 3).setAnchor(GridBagConstraints.LINE_START).build()); @@ -848,7 +894,7 @@ final class UnitConverterGUI { sigDigSlider.setPaintLabels(true); sigDigSlider.addChangeListener( - e -> this.presenter.setSignificantFigures(sigDigSlider.getValue())); + e -> this.presenter.setPrecision(sigDigSlider.getValue())); } { // prefix repetition settings @@ -861,15 +907,18 @@ final class UnitConverterGUI { final ButtonGroup prefixRuleButtons = new ButtonGroup(); final JRadioButton noRepetition = new JRadioButton("No Repetition"); - noRepetition.setSelected(true); + noRepetition.setEnabled(false); prefixRuleButtons.add(noRepetition); prefixRepetitionPanel.add(noRepetition, new GridBagBuilder(0, 0).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton noRestriction = new JRadioButton("No Restriction"); + noRestriction.setSelected(true); + noRestriction.setEnabled(false); prefixRuleButtons.add(noRestriction); prefixRepetitionPanel.add(noRestriction, new GridBagBuilder(0, 1).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton customRepetition = new JRadioButton("Custom Repetition Rule"); + customRepetition.setEnabled(false); prefixRuleButtons.add(customRepetition); prefixRepetitionPanel.add(customRepetition, new GridBagBuilder(0, 2).setAnchor(GridBagConstraints.LINE_START).build()); } @@ -884,19 +933,22 @@ final class UnitConverterGUI { final ButtonGroup searchRuleButtons = new ButtonGroup(); final JRadioButton noPrefixes = new JRadioButton("Never Include Prefixed Units"); - noPrefixes.setSelected(true); + noPrefixes.setEnabled(false); searchRuleButtons.add(noPrefixes); searchingPanel.add(noPrefixes, new GridBagBuilder(0, 0).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton fixedPrefixes = new JRadioButton("Include Some Prefixes"); + fixedPrefixes.setEnabled(false); searchRuleButtons.add(fixedPrefixes); searchingPanel.add(fixedPrefixes, new GridBagBuilder(0, 1).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton explicitPrefixes = new JRadioButton("Include Explicit Prefixes"); + explicitPrefixes.setEnabled(false); searchRuleButtons.add(explicitPrefixes); searchingPanel.add(explicitPrefixes, new GridBagBuilder(0, 2).setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton alwaysInclude = new JRadioButton("Include All Single Prefixes"); + alwaysInclude.setEnabled(false); searchRuleButtons.add(alwaysInclude); searchingPanel.add(alwaysInclude, new GridBagBuilder(0, 3).setAnchor(GridBagConstraints.LINE_START).build()); } @@ -909,9 +961,11 @@ final class UnitConverterGUI { final JCheckBox showAllVariations = new JCheckBox("Show Symbols in \"Convert Units\""); showAllVariations.setSelected(true); + showAllVariations.setEnabled(false); miscPanel.add(showAllVariations, new GridBagBuilder(0, 0).setAnchor(GridBagConstraints.LINE_START).build()); final JButton unitFileButton = new JButton("Manage Unit Data Files"); + unitFileButton.setEnabled(false); miscPanel.add(unitFileButton, new GridBagBuilder(0, 1).setAnchor(GridBagConstraints.LINE_START).build()); } } |