fix for entering decimal numbers

This commit is contained in:
Sergey Solovyev 2011-10-24 11:14:59 +04:00
parent d64ffef310
commit d0435877af
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" <manifest xmlns:a="http://schemas.android.com/apk/res/android"
package="org.solovyev.android.calculator" package="org.solovyev.android.calculator"
a:versionCode="3" a:versionCode="4"
a:versionName="1.1.1"> a:versionName="1.1.2">
<uses-sdk a:minSdkVersion="4" <uses-sdk a:minSdkVersion="4"
a:targetSdkVersion="8"/> a:targetSdkVersion="8"/>

View File

@ -25,7 +25,7 @@ public class CalculatorDisplay extends TextView {
private boolean valid = true; private boolean valid = true;
@NotNull @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) { public CalculatorDisplay(Context context) {
super(context); super(context);

View File

@ -26,7 +26,7 @@ public class CalculatorEditor extends EditText {
private boolean highlightText = true; private boolean highlightText = true;
@NotNull @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) { public CalculatorEditor(Context context) {
super(context); super(context);

View File

@ -61,9 +61,11 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result> {
private final int colorRed; private final int colorRed;
private final int colorGreen; private final int colorGreen;
private final int colorBlue; private final int colorBlue;
private final boolean simpleFormat;
public TextHighlighter(int baseColor) { public TextHighlighter(int baseColor, boolean simpleFormat) {
this.color = baseColor; this.color = baseColor;
this.simpleFormat = simpleFormat;
//this.colorRed = Color.red(baseColor); //this.colorRed = Color.red(baseColor);
this.colorRed = (baseColor >> 16) & 0xFF; this.colorRed = (baseColor >> 16) & 0xFF;
//this.colorGreen = Color.green(baseColor); //this.colorGreen = Color.green(baseColor);
@ -84,7 +86,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result> {
int numberOffset = 0; int numberOffset = 0;
final NumberBuilder numberBuilder = new NumberBuilder(); final NumberBuilder numberBuilder = new NumberBuilder(simpleFormat);
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
MathType.Result mathType = MathType.getType(text, i); MathType.Result mathType = MathType.getType(text, i);

View File

@ -78,12 +78,21 @@ public enum CalculatorEngine {
@NotNull @NotNull
public String format(@NotNull Double value) { public String format(@NotNull Double value) {
return format(value, true);
}
@NotNull
public String format(@NotNull Double value, boolean round) {
if (!value.isInfinite() && !value.isNaN()) { if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat(); final DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(decimalGroupSymbols); df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(useGroupingSeparator); df.setGroupingUsed(useGroupingSeparator);
df.setMaximumFractionDigits(instance.getPrecision()); if (round) {
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue()); df.setMaximumFractionDigits(instance.getPrecision());
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return df.format(value);
}
} else { } else {
return String.valueOf(value); return String.valueOf(value);
} }

View File

@ -19,11 +19,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
public String process(@NotNull String s) throws ParseException { public String process(@NotNull String s) throws ParseException {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
final NumberBuilder numberBuilder = new NumberBuilder(); final NumberBuilder numberBuilder = new NumberBuilder(true);
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i); final MathType.Result mathTypeResult = MathType.getType(s, i);
MathType.Result mathTypeResult = MathType.getType(s, i);
numberBuilder.process(sb, mathTypeResult, null); numberBuilder.process(sb, mathTypeResult, null);

View File

@ -25,6 +25,12 @@ public class NumberBuilder {
@Nullable @Nullable
private String number = null; private String number = null;
private final boolean simpleFormat;
public NumberBuilder(boolean simpleFormat) {
this.simpleFormat = simpleFormat;
}
@NotNull @NotNull
public MathType.Result process(@NotNull StringBuilder sb, @NotNull MathType.Result mathTypeResult, @Nullable MutableObject<Integer> numberOffset) { public MathType.Result process(@NotNull StringBuilder sb, @NotNull MathType.Result mathTypeResult, @Nullable MutableObject<Integer> numberOffset) {
number = null; number = null;
@ -90,8 +96,28 @@ public class NumberBuilder {
result = new MathType.Result(MathType.constant, var.getName()); result = new MathType.Result(MathType.constant, var.getName());
} else { } else {
sb.delete(sb.length() - number.length() - numberOfGroupingSeparators, sb.length()); 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); numberOffset.setObject(formattedNumber.length() - number.length() - numberOfGroupingSeparators);
} }
sb.append(formattedNumber); sb.append(formattedNumber);

View File

@ -22,7 +22,7 @@ public class TextHighlighterTest {
@Test @Test
public void testProcess() throws Exception { 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()); final Random random = new Random(new Date().getTime());
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {