summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Boehm <hboehm@google.com>2015-09-03 16:04:50 -0700
committerHans Boehm <hboehm@google.com>2015-10-11 06:29:29 +0000
commitec7517f38758f67233f2b06bc7bab17a8ffeccba (patch)
treeb619bffb5e972bb4f46e9a4dca78ba16bcef61d0
parente4b8ff7c9c8a041ab451cdc78a019b2a4a666b99 (diff)
downloadandroid_packages_apps_ExactCalculator-ec7517f38758f67233f2b06bc7bab17a8ffeccba.tar.gz
android_packages_apps_ExactCalculator-ec7517f38758f67233f2b06bc7bab17a8ffeccba.tar.bz2
android_packages_apps_ExactCalculator-ec7517f38758f67233f2b06bc7bab17a8ffeccba.zip
Check for decimal point after adding ellipsis
Bug: 23728152 This avoids the one case in which we displayed neither a decimal point nor an exponent, and the decimal point was not just to the right of the number. Remove a redundant test, which obscured the logic. Add a couple of suggested manual tests that helped me find a (newly introduced, not submitted) bug. Change-Id: Iead1efa0a4e8ea74512e9e1f77334f80bbcdd202 (cherry picked from commit 73ecff20d9dfd80d1cff09e0210d8987049b9df3)
-rw-r--r--src/com/android/calculator2/CalculatorResult.java70
-rw-r--r--tests/README.txt2
2 files changed, 37 insertions, 35 deletions
diff --git a/src/com/android/calculator2/CalculatorResult.java b/src/com/android/calculator2/CalculatorResult.java
index 289e13d..5c96867 100644
--- a/src/com/android/calculator2/CalculatorResult.java
+++ b/src/com/android/calculator2/CalculatorResult.java
@@ -370,8 +370,12 @@ public class CalculatorResult extends AlignedTextView {
boolean negative, int lastDisplayedOffset[], boolean forcePrecision) {
final int minusSpace = negative ? 1 : 0;
final int msdIndex = truncated ? -1 : getNaiveMsdIndexOf(in); // INVALID_MSD is OK.
- final int decIndex = in.indexOf('.');
String result = in;
+ if (truncated || (negative && result.charAt(0) != '-')) {
+ result = KeyMaps.ELLIPSIS + result.substring(1, result.length());
+ // Ellipsis may be removed again in the type(1) scientific notation case.
+ }
+ final int decIndex = result.indexOf('.');
lastDisplayedOffset[0] = precOffset;
if ((decIndex == -1 || msdIndex != Evaluator.INVALID_MSD
&& msdIndex - decIndex > MAX_LEADING_ZEROES + 1) && precOffset != -1) {
@@ -400,42 +404,38 @@ public class CalculatorResult extends AlignedTextView {
exponent = initExponent + resLen - msdIndex - 1;
hasPoint = true;
}
- if (exponent != 0 || truncated) {
- // Actually add the exponent of either type:
- if (!forcePrecision) {
- int dropDigits; // Digits to drop to make room for exponent.
- if (hasPoint) {
- // Type (1) exponent.
- // Drop digits even if there is room. Otherwise the scrolling gets jumpy.
- dropDigits = expLen(exponent);
- if (dropDigits >= result.length() - 1) {
- // Jumpy is better than no mantissa. Probably impossible anyway.
- dropDigits = Math.max(result.length() - 2, 0);
- }
- } else {
- // Type (2) exponent.
- // Exponent depends on the number of digits we drop, which depends on
- // exponent ...
- for (dropDigits = 2; expLen(initExponent + dropDigits) > dropDigits;
- ++dropDigits) {}
- exponent = initExponent + dropDigits;
- if (precOffset - dropDigits > mLsdOffset) {
- // This can happen if e.g. result = 10^40 + 10^10
- // It turns out we would otherwise display ...10e9 because it takes
- // the same amount of space as ...1e10 but shows one more digit.
- // But we don't want to display a trailing zero, even if it's free.
- ++dropDigits;
- ++exponent;
- }
+ // Exponent can't be zero.
+ // Actually add the exponent of either type:
+ if (!forcePrecision) {
+ int dropDigits; // Digits to drop to make room for exponent.
+ if (hasPoint) {
+ // Type (1) exponent.
+ // Drop digits even if there is room. Otherwise the scrolling gets jumpy.
+ dropDigits = expLen(exponent);
+ if (dropDigits >= result.length() - 1) {
+ // Jumpy is better than no mantissa. Probably impossible anyway.
+ dropDigits = Math.max(result.length() - 2, 0);
+ }
+ } else {
+ // Type (2) exponent.
+ // Exponent depends on the number of digits we drop, which depends on
+ // exponent ...
+ for (dropDigits = 2; expLen(initExponent + dropDigits) > dropDigits;
+ ++dropDigits) {}
+ exponent = initExponent + dropDigits;
+ if (precOffset - dropDigits > mLsdOffset) {
+ // This can happen if e.g. result = 10^40 + 10^10
+ // It turns out we would otherwise display ...10e9 because it takes
+ // the same amount of space as ...1e10 but shows one more digit.
+ // But we don't want to display a trailing zero, even if it's free.
+ ++dropDigits;
+ ++exponent;
}
- result = result.substring(0, result.length() - dropDigits);
- lastDisplayedOffset[0] -= dropDigits;
}
- result = result + "E" + Integer.toString(exponent);
- } // else don't add zero exponent
- }
- if (truncated || negative && result.charAt(0) != '-') {
- result = KeyMaps.ELLIPSIS + result.substring(1, result.length());
+ result = result.substring(0, result.length() - dropDigits);
+ lastDisplayedOffset[0] -= dropDigits;
+ }
+ result = result + "E" + Integer.toString(exponent);
}
return result;
}
diff --git a/tests/README.txt b/tests/README.txt
index b5e6dfe..304766f 100644
--- a/tests/README.txt
+++ b/tests/README.txt
@@ -39,6 +39,8 @@ Some interesting manual test cases:
-10^30 - 10^10
-1.2x10^-9
-1.2x10^-8
+-1.2x10^-10
+-10^-12
1 - 10^-98
1 - 10^-100
1 - 10^-300