round preference
This commit is contained in:
@@ -481,10 +481,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) {
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
|
||||
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
|
||||
|
||||
CalculatorEngine.instance.reset(this, preferences);
|
||||
if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) ||
|
||||
CalculatorEngine.ROUND_RESULT_P_KEY.equals(key) ||
|
||||
CalculatorEngine.RESULT_PRECISION_P_KEY.equals(key)) {
|
||||
CalculatorEngine.instance.reset(this, preferences);
|
||||
this.calculatorModel.evaluate();
|
||||
}
|
||||
|
||||
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
|
||||
assert colorExpressionsInBracketsDefault != null;
|
||||
|
@@ -5,20 +5,33 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceActivity;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 7/16/11
|
||||
* Time: 6:37 PM
|
||||
*/
|
||||
public class CalculatorPreferencesActivity extends PreferenceActivity {
|
||||
public class CalculatorPreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
addPreferencesFromResource(R.xml.main_preferences);
|
||||
|
||||
final SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (CalculatorEngine.ROUND_RESULT_P_KEY.equals(key)) {
|
||||
findPreference(CalculatorEngine.RESULT_PRECISION_P_KEY).setEnabled(preferences.getBoolean(key, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -44,8 +44,11 @@ public enum CalculatorEngine {
|
||||
public static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
|
||||
private static final String GROUPING_SEPARATOR_DEFAULT = " ";
|
||||
|
||||
private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
|
||||
private static final String RESULT_PRECISION_DEFAULT = "5";
|
||||
public static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result";
|
||||
public static final boolean ROUND_RESULT_DEFAULT = true;
|
||||
|
||||
public static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
|
||||
public static final String RESULT_PRECISION_DEFAULT = "5";
|
||||
|
||||
@NotNull
|
||||
private Interpreter interpreter;
|
||||
@@ -53,6 +56,8 @@ public enum CalculatorEngine {
|
||||
@NotNull
|
||||
private final Object lock = new Object();
|
||||
|
||||
private boolean roundResult = true;
|
||||
|
||||
private int precision = 5;
|
||||
|
||||
@NotNull
|
||||
@@ -87,9 +92,13 @@ public enum CalculatorEngine {
|
||||
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());
|
||||
if (round ) {
|
||||
if (isRoundResult()) {
|
||||
df.setMaximumFractionDigits(instance.getPrecision());
|
||||
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
|
||||
} else {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
} else {
|
||||
return df.format(value);
|
||||
}
|
||||
@@ -217,7 +226,7 @@ public enum CalculatorEngine {
|
||||
}
|
||||
}
|
||||
|
||||
public int getPrecision() {
|
||||
private int getPrecision() {
|
||||
return precision;
|
||||
}
|
||||
|
||||
@@ -225,6 +234,14 @@ public enum CalculatorEngine {
|
||||
this.precision = precision;
|
||||
}
|
||||
|
||||
private boolean isRoundResult() {
|
||||
return roundResult;
|
||||
}
|
||||
|
||||
public void setRoundResult(boolean roundResult) {
|
||||
this.roundResult = roundResult;
|
||||
}
|
||||
|
||||
public void init(@Nullable Context context, @Nullable SharedPreferences preferences) throws EvalError {
|
||||
synchronized (lock) {
|
||||
reset(context, preferences);
|
||||
@@ -238,6 +255,7 @@ public enum CalculatorEngine {
|
||||
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
|
||||
//noinspection ConstantConditions
|
||||
this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
|
||||
this.setRoundResult(preferences.getBoolean(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT));
|
||||
|
||||
final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT);
|
||||
if (StringUtils.isEmpty(groupingSeparator)) {
|
||||
|
Reference in New Issue
Block a user