fix for entering decimal numbers

This commit is contained in:
serso 2011-10-24 11:14:59 +04:00
parent 7705e5d9b6
commit 1ec242bb1a
8 changed files with 50 additions and 15 deletions

View File

@ -2,8 +2,8 @@
<manifest xmlns:a="http://schemas.android.com/apk/res/android"
package="org.solovyev.android.calculator"
a:versionCode="3"
a:versionName="1.1.1">
a:versionCode="4"
a:versionName="1.1.2">
<uses-sdk a:minSdkVersion="4"
a:targetSdkVersion="8"/>

View File

@ -25,7 +25,7 @@ public class CalculatorDisplay extends TextView {
private boolean valid = true;
@NotNull
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE);
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE, true);
public CalculatorDisplay(Context context) {
super(context);

View File

@ -26,7 +26,7 @@ public class CalculatorEditor extends EditText {
private boolean highlightText = true;
@NotNull
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE);
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE, false);
public CalculatorEditor(Context context) {
super(context);

View File

@ -61,9 +61,11 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result> {
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<TextHighlighter.Result> {
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);

View File

@ -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);
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);
}

View File

@ -19,11 +19,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
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);

View File

@ -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<Integer> 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);

View File

@ -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++) {