From d0435877af8e5046e7752c47b081ce555814a181 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Mon, 24 Oct 2011 11:14:59 +0400 Subject: [PATCH] fix for entering decimal numbers --- AndroidManifest.xml | 4 +-- .../android/calculator/CalculatorDisplay.java | 2 +- .../android/calculator/CalculatorEditor.java | 2 +- .../android/calculator/TextHighlighter.java | 6 ++-- .../calculator/model/CalculatorEngine.java | 13 ++++++-- .../model/FromJsclSimplifyTextProcessor.java | 6 ++-- .../calculator/model/NumberBuilder.java | 30 +++++++++++++++++-- .../calculator/TextHighlighterTest.java | 2 +- 8 files changed, 50 insertions(+), 15 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 0e210d0e..00b5e9e9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,8 +2,8 @@ + a:versionCode="4" + a:versionName="1.1.2"> diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java b/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java index 3fa78fad..c914ce51 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java @@ -25,7 +25,7 @@ public class CalculatorDisplay extends TextView { private boolean valid = true; @NotNull - private final static TextProcessor textHighlighter = new TextHighlighter(Color.WHITE); + private final static TextProcessor textHighlighter = new TextHighlighter(Color.WHITE, true); public CalculatorDisplay(Context context) { super(context); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java index 661cba37..bfe91912 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java @@ -26,7 +26,7 @@ public class CalculatorEditor extends EditText { private boolean highlightText = true; @NotNull - private final static TextProcessor textHighlighter = new TextHighlighter(Color.WHITE); + private final static TextProcessor textHighlighter = new TextHighlighter(Color.WHITE, false); public CalculatorEditor(Context context) { super(context); diff --git a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java index 1dd1c6f2..dae13291 100644 --- a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java +++ b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java @@ -61,9 +61,11 @@ public class TextHighlighter implements TextProcessor { private final int colorRed; private final int colorGreen; private final int colorBlue; + private final boolean simpleFormat; - public TextHighlighter(int baseColor) { + public TextHighlighter(int baseColor, boolean simpleFormat) { this.color = baseColor; + this.simpleFormat = simpleFormat; //this.colorRed = Color.red(baseColor); this.colorRed = (baseColor >> 16) & 0xFF; //this.colorGreen = Color.green(baseColor); @@ -84,7 +86,7 @@ public class TextHighlighter implements TextProcessor { int numberOffset = 0; - final NumberBuilder numberBuilder = new NumberBuilder(); + final NumberBuilder numberBuilder = new NumberBuilder(simpleFormat); for (int i = 0; i < text.length(); i++) { MathType.Result mathType = MathType.getType(text, i); diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index 72eda675..4cdd0210 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -78,12 +78,21 @@ public enum CalculatorEngine { @NotNull public String format(@NotNull Double value) { + return format(value, true); + } + + @NotNull + public String format(@NotNull Double value, boolean round) { if (!value.isInfinite() && !value.isNaN()) { final DecimalFormat df = new DecimalFormat(); df.setDecimalFormatSymbols(decimalGroupSymbols); df.setGroupingUsed(useGroupingSeparator); - df.setMaximumFractionDigits(instance.getPrecision()); - return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue()); + if (round) { + df.setMaximumFractionDigits(instance.getPrecision()); + return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue()); + } else { + return df.format(value); + } } else { return String.valueOf(value); } diff --git a/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java index 93077fc4..58ab385a 100644 --- a/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java @@ -19,11 +19,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor { public String process(@NotNull String s) throws ParseException { final StringBuilder sb = new StringBuilder(); - final NumberBuilder numberBuilder = new NumberBuilder(); + final NumberBuilder numberBuilder = new NumberBuilder(true); for (int i = 0; i < s.length(); i++) { - char ch = s.charAt(i); - - MathType.Result mathTypeResult = MathType.getType(s, i); + final MathType.Result mathTypeResult = MathType.getType(s, i); numberBuilder.process(sb, mathTypeResult, null); diff --git a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java index eaa298fa..8d7e3855 100644 --- a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java +++ b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java @@ -25,6 +25,12 @@ public class NumberBuilder { @Nullable private String number = null; + private final boolean simpleFormat; + + public NumberBuilder(boolean simpleFormat) { + this.simpleFormat = simpleFormat; + } + @NotNull public MathType.Result process(@NotNull StringBuilder sb, @NotNull MathType.Result mathTypeResult, @Nullable MutableObject numberOffset) { number = null; @@ -90,8 +96,28 @@ public class NumberBuilder { result = new MathType.Result(MathType.constant, var.getName()); } else { sb.delete(sb.length() - number.length() - numberOfGroupingSeparators, sb.length()); - final String formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number)); - if ( numberOffset != null ) { + + final String formattedNumber; + + if (!simpleFormat) { + int indexOfDot = number.indexOf('.'); + + if (indexOfDot < 0) { + formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number), false); + } else { + String integerPart = null; + if (indexOfDot != 0) { + integerPart = CalculatorEngine.instance.format(Double.valueOf(number.substring(0, indexOfDot)), false); + } else { + integerPart = ""; + } + formattedNumber = integerPart + number.substring(indexOfDot); + } + } else { + formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number), true); + } + + if (numberOffset != null) { numberOffset.setObject(formattedNumber.length() - number.length() - numberOfGroupingSeparators); } sb.append(formattedNumber); diff --git a/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java b/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java index acc0e4d7..17872ffd 100644 --- a/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java +++ b/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java @@ -22,7 +22,7 @@ public class TextHighlighterTest { @Test public void testProcess() throws Exception { - final TextProcessor textHighlighter = new TextHighlighter(0); + final TextProcessor textHighlighter = new TextHighlighter(0, true); final Random random = new Random(new Date().getTime()); for (int i = 0; i < 1000; i++) {