This commit is contained in:
serso
2011-10-22 19:31:35 +04:00
parent 8fbfbe120b
commit c5f8633940
18 changed files with 80 additions and 39 deletions

View File

@@ -13,9 +13,6 @@ import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.calculator.model.TextProcessor;
import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
* User: serso
* Date: 10/6/11
@@ -32,7 +29,7 @@ class FromJsclNumericTextProcessor implements TextProcessor<String> {
if (doubleValue.isInfinite()) {
result = MathType.INFINITY;
} else {
result = format(doubleValue);
result = CalculatorEngine.instance.format(doubleValue);
}
} catch (NumberFormatException e) {
result = result.replace(MathType.INFINITY_JSCL, MathType.INFINITY);
@@ -53,18 +50,7 @@ class FromJsclNumericTextProcessor implements TextProcessor<String> {
}
private String format(@NotNull String value) {
return format(Double.valueOf(value));
}
private String format(@NotNull Double value) {
if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);
df.setMaximumFractionDigits(CalculatorEngine.instance.getPrecision());
return df.format(new BigDecimal(value).setScale(CalculatorEngine.instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return String.valueOf(value);
}
return CalculatorEngine.instance.format(Double.valueOf(value));
}
protected String createResultForComplexNumber(@NotNull final String s) {

View File

@@ -21,7 +21,11 @@ import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Formatter;
import org.solovyev.common.utils.MutableObject;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -56,6 +60,27 @@ public enum CalculatorEngine {
@NotNull
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
@NotNull
private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
@NotNull
public String format(@NotNull Double value) {
if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(true);
df.setMaximumFractionDigits(instance.getPrecision());
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return String.valueOf(value);
}
}
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(' ');
}
public String evaluate(@NotNull JsclOperation operation,
@NotNull String expression) throws EvalError, ParseException {
return evaluate(operation, expression, null);
@@ -191,6 +216,12 @@ public enum CalculatorEngine {
}
}
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
synchronized (lock) {
this.decimalGroupSymbols = decimalGroupSymbols;
}
}
@NotNull
public VarsRegister getVarsRegister() {
return varsRegister;

View File

@@ -27,6 +27,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
String number = null;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if ( Character.isWhitespace(ch) ) {
continue;
}
mathTypeResult = MathType.getType(s, i);
@@ -48,6 +51,8 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
}
numberBuilder = null;
} else {
number = null;
}
}
@@ -73,6 +78,8 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
}
numberBuilder = null;
} else {
number = null;
}
replaceSystemVars(sb, number);
@@ -171,6 +178,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
sb.delete(sb.length() - number.length(), sb.length());
sb.append(var.getName());
result = MathType.constant;
} else {
sb.delete(sb.length() - number.length(), sb.length());
sb.append(CalculatorEngine.instance.format(Double.valueOf(number)));
}
}

View File

@@ -33,6 +33,10 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
char ch = s.charAt(i);
startsWithFinder.setI(i);
if ( Character.isWhitespace(ch)) {
continue;
}
mathTypeResult = checkMultiplicationSignBeforeFunction(sb, s, i, mathTypeResult);
final MathType mathType = mathTypeResult.getMathType();