diff --git a/res/values-ru/text_strings.xml b/res/values-ru/text_strings.xml index 0f542a07..62619a59 100644 --- a/res/values-ru/text_strings.xml +++ b/res/values-ru/text_strings.xml @@ -20,6 +20,7 @@ Основные настройки Настройки вычислений Настройки внешнего вида + Другие настройки Настройки кнопок @@ -41,6 +42,8 @@ Подсветка выражений Округление результата Включает/выключает округление результата + Результат в научной форме + Если включено - результат будет всегда записан в научной форме (12.34E-12) Точность результата Выход Добавить diff --git a/res/values/preferences.xml b/res/values/preferences.xml index 3e8fda2f..cc02369c 100644 --- a/res/values/preferences.xml +++ b/res/values/preferences.xml @@ -33,6 +33,9 @@ org.solovyev.android.calculator.CalculatorModel_round_result true + calculation.output.science_notation + false + org.solovyev.android.calculator.CalculatorModel_functions org.solovyev.android.calculator.CalculatorModel_vars org.solovyev.android.calculator.CalculatorModel_history diff --git a/res/values/text_strings.xml b/res/values/text_strings.xml index 5ac941cd..5c54c916 100644 --- a/res/values/text_strings.xml +++ b/res/values/text_strings.xml @@ -20,6 +20,7 @@ Main settings Calculation settings Appearance settings + Other settings Drag buttons settings @@ -41,6 +42,8 @@ Highlight expressions Round result Toggles rounding of the result + Always scientific notation + If turned on forces to use only scientific notation of output (12.34E-12) Precision of result Exit Add diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 78da14f5..ef3d6ce1 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -8,13 +8,13 @@ a:title="@string/c_calc_ad_free_title" a:summary="@string/c_calc_ad_free_summary" a:defaultValue="false" - /> + /> - + - + + + - - - + - - - - + + + + + + + + - - + \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index 354ab955..ddd7b2b9 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -47,6 +47,9 @@ public enum CalculatorEngine { private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign"; private static final String MULTIPLICATION_SIGN_DEFAULT = "×"; + private static final String SCIENCE_NOTATION_P_KEY = "calculation.output.science_notation"; + private static final boolean SCIENCE_NOTATION_DEFAULT = false; + private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result"; private static final boolean ROUND_RESULT_DEFAULT = true; @@ -60,14 +63,16 @@ public enum CalculatorEngine { private static final String ANGLE_UNITS_DEFAULT = "deg"; public static class Preferences { - public static final Preference groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); - public static final Preference multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT); - public static final Preference precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper(Integer.class)); - public static final Preference roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT); - public static final Preference numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class)); - public static final Preference angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class)); + public static final Preference groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); + public static final Preference multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT); + public static final Preference precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper(Integer.class)); + public static final Preference roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT); + public static final Preference numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class)); + public static final Preference angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class)); + public static final Preference scienceNotation = new BooleanPreference(SCIENCE_NOTATION_P_KEY, SCIENCE_NOTATION_DEFAULT); private static final List preferenceKeys = new ArrayList(); + static { preferenceKeys.add(groupingSeparator.getKey()); preferenceKeys.add(multiplicationSign.getKey()); @@ -75,11 +80,12 @@ public enum CalculatorEngine { preferenceKeys.add(roundResult.getKey()); preferenceKeys.add(numeralBase.getKey()); preferenceKeys.add(angleUnit.getKey()); + preferenceKeys.add(scienceNotation.getKey()); } @NotNull public static List getPreferenceKeys() { - return Collections.unmodifiableList(preferenceKeys); + return Collections.unmodifiableList(preferenceKeys); } } @@ -255,7 +261,7 @@ public enum CalculatorEngine { if (parseExceptionObject != null || evalExceptionObject != null) { if (operation == JsclOperation.numeric && - ( preparedExpression.isExistsUndefinedVar() || ( evalExceptionObject != null && evalExceptionObject.getCause() instanceof NumeralBaseException)) ) { + (preparedExpression.isExistsUndefinedVar() || (evalExceptionObject != null && evalExceptionObject.getCause() instanceof NumeralBaseException))) { return evaluate(JsclOperation.simplify, expression, mr); } @@ -296,21 +302,7 @@ public enum CalculatorEngine { public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) { synchronized (lock) { - if (preferences != null) { - this.setPrecision(Preferences.precision.getPreference(preferences)); - this.setRoundResult(Preferences.roundResult.getPreference(preferences)); - this.setAngleUnits(getAngleUnitsFromPrefs(preferences)); - this.setNumeralBase(getNumeralBaseFromPrefs(preferences)); - this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); - - final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); - if (StringUtils.isEmpty(groupingSeparator)) { - this.getEngine().setUseGroupingSeparator(false); - } else { - this.getEngine().setUseGroupingSeparator(true); - this.getEngine().setGroupingSeparator(groupingSeparator.charAt(0)); - } - } + softReset(context, preferences); varsRegistry.load(context, preferences); functionsRegistry.load(context, preferences); @@ -327,6 +319,7 @@ public enum CalculatorEngine { this.setAngleUnits(getAngleUnitsFromPrefs(preferences)); this.setNumeralBase(getNumeralBaseFromPrefs(preferences)); this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); + this.setScienceNotation(Preferences.scienceNotation.getPreference(preferences)); final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); if (StringUtils.isEmpty(groupingSeparator)) { @@ -391,6 +384,10 @@ public enum CalculatorEngine { getEngine().setAngleUnits(angleUnits); } + public void setScienceNotation(boolean scienceNotation) { + getEngine().setScienceNotation(scienceNotation); + } + public void setNumeralBase(@NotNull NumeralBase numeralBase) { getEngine().setNumeralBase(numeralBase); } diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index f80afbec..b52bf2b3 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -261,6 +261,14 @@ public class CalculatorEngineTest { Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.getEngine().simplify("∫(log(x, 10), x)")); } + @Test + public void testFormatting() throws Exception { + final CalculatorEngine ce = CalculatorEngine.instance; + + Assert.assertEquals("12 345", ce.evaluate(JsclOperation.simplify, "12345").getResult()); + + } + @Test public void testI() throws CalculatorParseException, CalculatorEvalException { final CalculatorEngine cm = CalculatorEngine.instance;